We are using Razor outside of the typical MVC flow. 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?
Razor is a standard markup syntax that allows us to embed server code into the web pages. It uses its own syntax and keywords to generate view. If there is server code in the web page, server executes that code first then send response to the browser. It allows us to perform logical tasks in the view page.
All Razor files end with . cshtml. Most Razor files are intended to be browsable and contain a mixture of client-side and server-side code, which, when processed, results in HTML being sent to the browser. These pages are usually referred to as "content pages".
Razor directives are represented by implicit expressions with reserved keywords following the @ symbol. A directive typically changes the way a view is parsed or enables different functionality. Understanding how Razor generates code for a view makes it easier to understand how directives work. CSHTML Copy.
The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With