开发者

HTMLHelpers from MVC2 compatibiliy with Razor

Will开发者_C百科 existing HTMLHelpers work with Razor? If so, does Razor change anything about either the design or usage of HTMLHelpers?

thx


Yes, existing helpers work perfectly fine with Razor. For example:

@Html.ActionLink("foo bar", "foo")

What Razor changes is that now you have the possibility to define inline helpers like this:

@helper FooBar(string foo)
{
    <div>Hello @foo</div>
}

And use like this:

@FooBar("World")

As far as classic HTML helpers are concerned, Razor changes nothing, it's just a view engine so you continue to write your helpers as you've always did:

public static class HtmlExtensions
{
    public static MvcHtmlString FooBar(this HtmlHelper htmlHelper, string value)
    {
        var builder = new TagBuilder("div");
        div.SetInnerText(value);
        return MvcHtmlString.Create(div.ToString());
    }
}

And use in Razor:

@Html.FooBar("some value")


Razor performs HTML Encoding by default.

So if any of your MVC2 HtmlHelpers emit markup, they might not work if they are returning String instead of MvcHtmlString.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜