Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Microsoft.AspNetCore.Html.HtmlString

Its a .Net Core 2.0 application running on Visual Studio 2017 I want to display an HTML string returned on the View.

I have added the Microsoft.AspNetCore.Html.Abstractions Nuget to my application, and I want to display an HTML sting on the Razor view, this is how my CSHTML looks like

@model EDMTLC.Models.QuickResultViewModel
@using Microsoft.AspNetCore.Html;
<div>@Html.Display(Model.ResultHTML) </div>
<div>TEST</div>

I don't think @Html.Display is the way I should be doing. In previous version .Net I used to to

<div>
    <text> @MvcHtmlString.Create(Model.ReportHTML)</text>
 </div>

but this will not work anymore with Core 2.0

Can someone help with an example? I found this thread, but it didn't help!

Thanks

like image 454
Srééjîth Náîr Avatar asked Mar 29 '18 23:03

Srééjîth Náîr


People also ask

What is the difference between Microsoft Netcore app and Aspnetcore?

NET Core is a runtime. It can execute applications that are built for it. ASP.NET Core is a collection of libraries that form a Framework for building web applications.

What is HTMLString?

Unlike most HTML parsers which generate tree structures, HTMLString generates a string of characters each with its own set of tags. This flat structure makes it easy to manipulate ranges (for example - text selected by a user) as each character is independent and doesn't rely on a hierarchical tag structure.


2 Answers

A more elegant solution is to build an extension method:

using Microsoft.AspNetCore.Html;
public static class StringExtensions
{
    /// <summary>
    /// Convert a standard string into a htmlstring for rendering in a view
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static HtmlString ToHtmlString(this string value)
    {
        return new HtmlString(value);
    }
}

In your _ViewImports file, include the namespace if required:

@using MyProject.Extensions

Now in your views you will be able to do:

<div>@Model.ReportHTML.ToHtmlString()</div>

I'd advise doing additional sanitization in a real world app but this should answer the OP's question.

like image 66
DonkeyKong Avatar answered Oct 13 '22 20:10

DonkeyKong


Have you tried @Html.Raw()? It should display the value as raw string, without encoding.

<pre>
    <code>@Html.Raw(Model.ReportHTML)</code>
</pre>

This should work in Asp.Net Core Mvc 2.0 and previous versions as well, without the need of installing additional NuGet packages.

Notes

  1. In order to use @Html.Display(), if you're passing a string expression into the argument, that argument has to match one of your properties defined in your view model.

    // This doesn't work
    @Html.Display(Model.ResultHTML)
    
    // This works but it encodes your HTML
    @Html.Display("ResultHtml")
    
  2. Or you can use @Html.DisplayFor() and pass a lamba expression to avoid magic strings in your View

    // This works but it encodes your HTML
    @Html.DisplayFor(x => x.ResultHTML)
    
like image 30
David Liang Avatar answered Oct 13 '22 19:10

David Liang