Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

UrlResolutionTagHelper+EncodeFirstSegmentContent net::ERR_ABORTED #7700

Closed
AHHejazi opened this issue Apr 23, 2018 · 5 comments
Closed

UrlResolutionTagHelper+EncodeFirstSegmentContent net::ERR_ABORTED #7700

AHHejazi opened this issue Apr 23, 2018 · 5 comments
Assignees
Labels

Comments

@AHHejazi
Copy link

AHHejazi commented Apr 23, 2018

Dears, we have this issue with MVC Core

https://stackoverflow.com/questions/49971207/urlresolutiontaghelperencodefirstsegmentcontent-neterr-aborted Please any help

@mkArtakMSFT
Copy link
Member

@AHHejazi, can you please share your code here? We will need a repro to investigate.

@AHHejazi
Copy link
Author

AHHejazi commented Apr 25, 2018

Dear we upload the project on github

https://github.com/AHHejazi/IdentityV1/tree/master/MOHIdentityApp

the main project is : Identity.WebApp

the Tagehelper project under Common/Framework.Web/

when you run the project you will get home page => right click and inspect =>in console you will get the error :

GET http://localhost:61233/Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper+EncodeFirstSegmentContent net::ERR_ABORTED

this error appear when we include the taghelper

@addTagHelper Framework.Web.TagHelpers, Framework.Web*

in _ViewImports.cshtml

view

but if i remove this line the the errors gone but the custom tag helpers will not work,thank in advance

@mkArtakMSFT
Copy link
Member

@NTaylorMullen, reassigning this to you to investigate. Thanks!

@NTaylorMullen
Copy link
Member

NTaylorMullen commented Apr 25, 2018

@AHHejazi really appreciate the repo, worked first try, no problems at all! So the issue you're encountering is actually in how your ScriptTagHelper reads src attribute values. The NToastNotify component generates a Script tag with a non-string attribute value so when you ToString its value it ends up getting the type reference of the value instead of the actual value. I filed an issue a while back about making this process more first class here. For the interim here's my take on your ScriptTagHelper that works as expected:

using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Framework.Web.TagHelpers
{
    // You may need to install the Microsoft.AspNetCore.Razor.Runtime package into your project
    [HtmlTargetElement("script", Attributes = "src")]
    public class ScriptTagHelper : TagHelper
    {
        private readonly IScriptManager _scriptManager;

        public ScriptTagHelper(IScriptManager scriptManager, HtmlEncoder htmlEncoder)
        {
            _scriptManager = scriptManager;
            HtmlEncoder = htmlEncoder;
        }

        public string IncludeOrderPriority { get; set; }

        private HtmlEncoder HtmlEncoder { get; }

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            // needed because of builtin tag helper that looks at src
            var src = GetEncodedStringValue(output.Attributes["src"].Value);
            _scriptManager.AddScript(new ScriptReference(src, Convert.ToInt32(IncludeOrderPriority)));

            await output.GetChildContentAsync();
            output.SuppressOutput();
        }

        private string GetEncodedStringValue(object attributeValue)
        {
            var stringValue = attributeValue as string;
            if (stringValue != null)
            {
                var encodedStringValue = HtmlEncoder.Encode(stringValue);
                return encodedStringValue;
            }
            else
            {
                var htmlContent = attributeValue as IHtmlContent;
                if (htmlContent != null)
                {
                    var htmlString = htmlContent as HtmlString;
                    if (htmlString != null)
                    {
                        // No need for a StringWriter in this case.
                        stringValue = htmlString.ToString();
                    }
                    else
                    {
                        using (var writer = new StringWriter())
                        {
                            htmlContent.WriteTo(writer, HtmlEncoder);
                            stringValue = writer.ToString();
                        }
                    }

                    return stringValue;
                }
            }

            return attributeValue.ToString();
        }
    }
}

Note: After this fix there still seems to be an issue with nToastNotify not being defined but i'll leave you to figure that one out 😉

@AHHejazi
Copy link
Author

Thank you too much

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants