With the new Razor View Engine, should my HtmlHelpers return string or IHtmlString?

血红的双手。 提交于 2019-12-03 11:37:37

问题


With the Razor View Engine, anytime you output a string directly to the page, it's HTML encoded. e.g.:

@"<p>Hello World</p>"

will actually get output to the page as:

&lt;p&gt;Hello World &lt;/p&gt;

Which would show up in the browser as:

<p>Hello World </p>

Here's the problem though, when creating Html helpers, till now, with the old aspx view engine I would just return a string, and output that to the browser:

<%= Html.MyCoolHelperMethod(); %>

So my question is basically this. Do I do this:

public static IHtmlString MyCoolHelperMethod(this HtmlHelper helper)
{
   return new helper.Raw("<p>Hello World</p>");
}

in which case I can just do this in my cshtml:

@Html.MyCoolHelperMethod();

or do I do this:

public static string MyCoolHelperMethod(this HtmlHelper helper)
{
   return "<p>Hello World</p>";
}

in which case I need to do the work in my cshtml:

@Html.Raw(Html.MyCoolHelperMethod());

Obviously the first approach makes the view look a lot cleaner, but I'm just wondering if the common pattern is in fact for helpers to return an IHtmlString and I've been doing it wrong in the past.


回答1:


In most cases you should return an instance of IHtmlString. That's the pattern followed by the built-in helpers* and it means that the consumer of a helper does not need to worry about under- or over-encoding.

Instead of using the Raw function you should probably just return a new instance of HtmlString.

public static IHtmlString MyCoolHelperMethod(this HtmlHelper helper) {
    return new HtmlString("<p>Hello World</p>");
}

*Note that MVC 3 actually uses MvcHtmlString as the return type of its helpers but this is a holdover from the MVC 2 days. (Complicated story, but in short, IHtmlString was only introduced in .NET 4 and since MVC 2 supported .NET 3.5 the MvcHtmlString type was introduced as an intermediate step). All helpers targetting MVC 3 and higher should return IHtmlString.



来源:https://stackoverflow.com/questions/4596834/with-the-new-razor-view-engine-should-my-htmlhelpers-return-string-or-ihtmlstri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!