Convert Raw HTML Format in ASP.NET MVC

The @Html.Raw() HTML helper in ASP.NET MVC allows you to output raw HTML in your views. Normally, when you output a string of HTML in a view, the HTML is encoded to prevent cross-site scripting (XSS) attacks. However, there may be times when you want to output raw HTML that you trust is safe.

The @Html.Raw() method takes a string parameter and returns an IHtmlString object, which represents an HTML-encoded string. When you use the @Html.Raw() method, the string is not encoded and is output as raw HTML. 

Example:-

@model MyProject.Models.MyViewModel


<div>

    @Html.Raw("<h1>" + Model.Title + "</h1>")

    @Html.Raw("<p>" + Model.Description + "</p>")

</div>

In the above example, @Html.Raw() is used to output raw HTML containing a <h1> and <p> tag with the Title and Description properties of the MyViewModel model.


Popular Posts