.NET Core - MVC

Observations about .NET Core, MVC and converting YetaWF from ASP.NET MVC5 to .NET Core MVC. The new .NET Core has great new features. Getting there from an existing MVC 5 app is HARD!

TagBuilder.InnerHtml

02/07/2017

If you're using TagBuilder extensively in any MVC5 project, you'll eventually use SetInnerText and InnerHtml.

These always felt a bit awkward to use. ASP.NET Core MVC fixes that, but with another somewhat odd InnerHtml property which uses IHtmlContentBuilder. This collects encoded and unencoded strings and renders the entire contents using WriteTo.

The end result of this "improvement" is that your existing code is incompatible. Enjoy! Maybe in the grand scheme of things (i.e., MVC6), this is the better way, but at the expense of compatibility. It's disappointing that this new version of ASP.NET Core MVC does not seem to care about compatibility.

Here is how you could get the inner html if you want it as a string:

namespace YetaWF.Core.Support {

    public static class TagBuilderExtension {
        ...clipped...
        public static string GetInnerHtml(this TagBuilder tagBuilder) {
            using (var writer = new System.IO.StringWriter()) {
                tagBuilder.InnerHtml.WriteTo(writer, HtmlEncoder.Default);
                return writer.ToString();
            }
        }
    }
}

No Comments

Be the first to add a comment!

Add New Comment

Complete this simple form to add a comment to this blog entry.