Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return custom html from ViewComponent

In an ASP.NET Core app I want to return custom html from a ViewComponent. I can return custom text, but the html will be encoded instead of being embedded:

public class BannerViewComponent : ViewComponent
{
  public async Task<IViewComponentResult> InvokeAsync(string param1, int param2)
  {
    return Content("<strong>some custom html</strong>");
  }
}

I use it in my .cshtml page:

    @await Component.InvokeAsync("BannerView")

On the page this will Show as <strong>some custom html</strong> instead of some custom html.
How do I directly return HTML instead of text from the ViewComponent?

like image 258
Sam Avatar asked Feb 01 '17 14:02

Sam


1 Answers

If you don't want to return a view you can return HTML this way without a view:

return new HtmlContentViewComponentResult(new HtmlString("Not bold - <b>bold</b>"));
like image 98
b.pell Avatar answered Oct 12 '22 00:10

b.pell