TagBuilder InnerHtml in ASP.NET 5 MVC 6

后端 未结 6 971
难免孤独
难免孤独 2021-02-07 01:10

It seems to me that there are major breaking changes in TagBuilder as of beta7 with no mention about them in the announcements repo.

Specifically .ToString no longer ren

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 01:37

    Building on @Mihai's answer to show the actual code for the StringWriter approach:

    // Create tag builder
    var builder = new TagBuilder("img");
    //...
    // Render tag approach also changed in .NetCore
    builder.TagRenderMode = TagRenderMode.SelfClosing;
    
    //Create the StringWriter and make TagBuilder "WriteTo" it
    var stringWriter = new System.IO.StringWriter();
    builder.WriteTo(stringWriter, HtmlEncoder.Default);
    var tagBuilderIsFinallyAStringNow = stringWriter.ToString();
    

提交回复
热议问题