Skip to content

IHtmlContent not rendering as HtmlString in MVC 5.2.7? #290

@kendallb

Description

@kendallb

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, and I recently updated it to the latest version that uses .net Standard 2.0, and since it is now targetting .net code it no longer implemented IHtmlString in the HtmlTag class. Rather it implements IHtmlContent, which is the new ASP.net Core version of that interface.

Well needless to say, when we use this library it does not work as it renders the HTML encoded onto the page, as it does not recognize it should be rendered as raw HTML.

So when the page writes an object, it does it like so:

    /// <summary>Writes the specified object as an HTML-encoded string.</summary>
    /// <param name="value">The object to encode and write.</param>
    public override void Write(object value) => WebPageExecutingBase.WriteTo(this.Output, value);

which then writes it to the output like so:

    /// <summary>Writes the specified object as an HTML-encoded string to the specified text writer.</summary>
    /// <param name="writer">The text writer.</param>
    /// <param name="content">The object to encode and write.</param>
    public static void WriteTo(TextWriter writer, object content) => writer.Write(HttpUtility.HtmlEncode(content));

So the problem is 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.

But really to support allowing folks to slowly migrate their tech stacks over to .net standard, and still have that code work with MVC 5.2.7, there really needs to be something that understands IHtmlContent and treats it the same as IHtmlString?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions