Render raw html from view component

Related searches

For very simple ViewComponents I'd like to bypass a view and just return an encoded HTML string.



My self-made implementation is like this:

public class RawHtmlResult : IViewComponentResult
    public RawHtmlResult(string EncodedHtml)
        _encodedHtml = EncodedHtml;
    public string _encodedHtml { get; set; }

    public void Execute(ViewComponentContext context)

    public Task ExecuteAsync(ViewComponentContext context)
        return context.Writer.WriteAsync(_encodedHtml);


public async Task<IViewComponentResult> InvokeAsync(long id)
    var someArray = await _someService(id);
    var ulHelper = new TagBuilder("ul");
    var liHelper = new TagBuilder("li");

    foreach (var setName in someArray)
        liHelper = new TagBuilder("li");
    var writer = new System.IO.StringWriter();
    ulHelper.WriteTo(writer, new HtmlEncoder());

    return new RawHtmlResult(writer.ToString());

So far I have not found a suitable IViewComponentResult. Do I miss apoint?

The Invoke() method on the view component does not need to return IViewComponentResult, it can return HtmlString so you don't need a custom implementation of IViewComponentResult.

For example:

public HtmlString Invoke()
    return new HtmlString(@"<b>Hello World</b>");

Improve support for returning raw HTML from view component's , Improve support for returning raw HTML from view component's `render` method #2261. Closed. nikazooz opened this issue on Jun 5 � 1 comment. Closed� A somewhat valid use-case might be if you’re writing a new front-end component for an ancient legacy system that (shudders) stores HTML mixed with data in an aging database in the long-forgotten server room in some leased facility upstate. In that case, you might have to resort to rendering raw HTML in your app. So let’s get started.

return Content("<ul><li>One</li><li>Two</li><ul>");

The method is Controller.Content. You can also use it to return content types other than HTML.

As mentioned it's not the preferred way to return HTML - typically the controller should return a model and the view should provide HTML. It might be preferable to use a partial view. But scenarios come up where we have to do things like this. (I just encountered one myself.)

Rendering raw HTML content � Issue #16469 � dotnet/aspnetcore , for a way to render raw HTML strings as part of a Blazor component. I'd like to display the generated HTML in my Blazor component. The HTML content (data) will be displayed using Html.Raw Helper method in ASP.Net MVC Razor. TAGs: ASP.Net, Rich Text Editor, HTML, MVC Here Mudassar Ahmed Khan has explained with an example, how to display HTML content (data) from Model in View in ASP.Net MVC Razor.

For future visitors:

The easiest way to return HtmlContentViewComponentResult from Invoke() method. HtmlContentViewComponentResult is one of 3 classes which implements IViewComponentResult interface.

Here is the example code:

public IViewComponentResult Invoke(object arguments)
    return new HtmlContentViewComponentResult(
        new HtmlString("<ul><li>My HTML code</li></ul>"));

As you may see HtmlContentViewComponentResult requires IHtmlContent as content to render on view, so best way is to use HtmlString class.

Still for more complex results it's better to return View instead of generating raw HTML programmatically.

Rendering raw HTML - Blazor Quick Start Guide, You're currently viewing a course logged out. @page "/renderhtml" <h1>Raw HTML Rendering in Blazor</h1> <hr /> @((MarkupString)customHTML)� To render a component from a page or view, use the Component Tag Helper. Prerequisites. Follow the guidance in the Prepare the app to use components in pages and views section of the Integrate ASP.NET Core Razor components into Razor Pages and MVC apps article. Component Tag Helper

Render Raw HTML In Your Vue Apps, Perhaps, for some reason you need to render raw HTML in your Vue components . Never fear: v-html is here. To use a view component as a Tag Helper, register the assembly containing the view component using the @addTagHelper directive. If your view component is in an assembly called MyWebApp, add the following directive to the _ViewImports.cshtml file: @addTagHelper *, MyWebApp

How to: Render ASP.NET MVC Razor Views to Strings, NET MVC is reserved purely for view rendering and generation of HTML output key components that are exposed on the view, such as the HttpContext intrinsic checks for HTML clients and returns either an HTML response or the raw data� Multiple render fragments. When we write mark-up inside a component, Blazor will assume it should be assigned to a Parameter on the component that is descended from the RenderFragment class and is named ChildContent. If we wish to use a different name, or multiple render fragments, then we must explicitly specify the parameter’s name in our

With View Components, you control what gets shared to your View Components. View Components are new to ASP.NET Core and are designed to do everything that a Partial View in previous versions of ASP.NET did and more. View Components are completely self-contained objects that consistently render html from a Razor view.

  • A View is raw html, mixed with markup. A controller should not have to do anything with rendering. Otherwise you shouldn't use MVC at all - it's the wrong pattern. Perhaps you are trying to solve a different problem? Why would you want to create raw HTML like this inside an MVC framework?
  • Maybe he is developing a CMS and his articlebody is stored as HTML in the database? ;)
  • You may also look into HtmlString. Used it a bit different answer for tag-helper content being encoded. Not sure though on how to use it inside a controller
  • @PanagiotisKanavos: You have been absolutely right about MVC usage and I agree. Still, sometimes you do not need to follow the path 100% and I can keep my codebase smaller not introducing another "partial view"... Returning Content("foo", "text/html"); does not work because ViewComponent only support returning String, HtmlString or IViewComponentResult. Thanks for you help!
  • @kdaveid Not in this case. You can easily return an array of values and use a loop in the View. Or use a proper model and pass the array as one of its properties. The total number of lines is less than what you already typed
  • You also need to specify the 2nd argument, content type: Content(html,"text/html")
  • The second argument is optional.
  • I was under the impression that if you omitted the content-type it would always return text/plain but I just checked and it returns the correct content-type. Maybe that's new since MVC3.
  • The question is about ASP.NET Core / MVC6. The provided link refers to old framework up to MVC5, so likely not valid as MVC6 is a complete rewrite w/o System.Web.* and IIS dependencies
  • This does not work: Returning Content("foo", "text/html"); does not work because ViewComponent only support returning String, HtmlString or IViewComponentResult. (see above)