Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IHtmlContent not rendering as HtmlString in .Net 4.8? #120

Open
kendallb opened this issue Nov 29, 2020 · 1 comment
Open

IHtmlContent not rendering as HtmlString in .Net 4.8? #120

kendallb opened this issue Nov 29, 2020 · 1 comment

Comments

@kendallb
Copy link
Contributor

I am in the process of migrating our code slowly over to .net Standard 2.0 for our supplemental libraries, so that we can eventually migrate our huge application stack over to ASP.Net Core and .Net 5. We have been using HtmlTags for years, so I am trying to use the latest version of the library, however for some reason when we render a tag into HTML with ASP.net MVC 4.8, it does not render as HTML. The difference is ASP.net 4.8 is looking for IHtmlString on the class, to determine if it should be rendered as raw HTML, however it ends up not finding that. The HtmlTag class now has IHtmlContent as the interface which is compatible with ASP.net Core, but for some reason the version of ASP.NET MVC we are using is not supporting that.

What is the solution to get this working, so that ASP.net 4.8 will recognize IHtmlContent and render it correctly?

@kendallb
Copy link
Contributor Author

Ok, so the problem is buried deep inside System.Web, as this is how HttpUtility.Encode() is implemented:

    /// <summary>Converts an object's string representation into an HTML-encoded string, and returns the encoded string.</summary>
    /// <param name="value">An object.</param>
    /// <returns>An encoded string.</returns>
    public static string HtmlEncode(object value)
    {
      if (value == null)
        return (string) null;
      return value is IHtmlString htmlString ? htmlString.ToHtmlString() : HttpUtility.HtmlEncode(Convert.ToString(value, (IFormatProvider) CultureInfo.CurrentCulture));
    }

Unfortunately there is nothing in there that knows about IHtmlContent. I don't see any updated packages for any of the related packages like Microsoft.AspNet.Mvc (5.2.7 is the latest), so there is no easy solution for this problem that I can think of. Best I can come up with at the moment is to derive my own view page for all our Razor views, and override the write function in there to understand IHtmlContent? It would work (and we already have our own custom base class to handle IoC), but it does mean if someone forgets to use it, stuff breaks.

I guess I will also open an issue on https://github.com/aspnet/AspNetWebStack

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

No branches or pull requests

1 participant