开发者

How to disable HTML Encoding for an entire Razor view

We are using Razor outside of the typical MVC flo开发者_如何学运维w. Our Razor views are called from within an XSL transform via a C# extension. The output of the Razor view is returned to the xsl transform as a string. In some cases we capture the result of a Razor view into an xsl variable and then pass that back out to our Model to be consumed as data in another Razor view. When this happens we end up with the first view being double encoded, once by Razor, the second time via the xsl transform. We need to be able to run Razor without having it encode the output.

Is this possible? How would we go about it?


Since you want to disable encoding in your entire view, your best bet would be to create your own view base class inheriting from WebPageBase (and then your views should use @inherits to specify your new type) and override the Write(object value) method so that it calls WriteLiteral() instead. That way the output will not be encoded.


Further the the answer above you would achieve this like so:

public abstract class TextBasedViewPage : System.Web.Mvc.WebViewPage
{
    public override void Write(object value)
    {
        WriteLiteral(value);
    }
}

public abstract class TextBasedViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
{
    public override void Write(object value)
    {
        WriteLiteral(value);
    }
}

Then in your view you can start out with:

@inherits MyNamespace.TextBasedViewPage<MyModelNamespace.Model>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜