2016-06-29 23 views
0

當我有一個ViewComponent它可以返回一個視圖。AspCore ViewComponent與StreamResult

但這綁定到一個文件中查看文件夾這樣

public class ContactViewComponent : ViewComponent 
{ 
    public IViewComponentResult Invoke() 
    { 
     return View(); 
    } 
} 

現在我想通過其中包含的觀點標記給定的字符串創建ViewContent。

有沒有一個內置的方式來做到這一點與視圖組件?

回答

1

HtmlContent返回HtmlContentViewComponentResult應該有所斬斷。

public class ContactViewComponent : ViewComponent 
{ 
    public IViewComponentResult Invoke() 
    { 
     var htmlString = new HtmlString("<b>Hello World!</b>"); 

     return new HtmlContentViewComponentResult(htmlString); 
    } 
} 

HtmlString返回原始字符串沒有逃脫它。但要小心,不要讓人們注入惡意代碼到這裏呈現的內容中!

鏈接到來源:

https://github.com/aspnet/Mvc/blob/a78f77afde003c4a3fcf5dd7b6dc13dd9c85f825/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/HtmlContentViewComponentResult.cs

https://github.com/aspnet/HtmlAbstractions/blob/dev/src/Microsoft.AspNetCore.Html.Abstractions/HtmlString.cs