RazorEngine issues with @Html

I am using RazorEngine to render out some basic content (a very crude content management system).

It works great until I include any @Html syntax into markup.

If the markup contains an @html I get the following error:

Unable to compile template. The name 'Html' does not exist in the current context

This is the view that renders the markup:

@Model Models.ContentPage

@{
    ViewBag.Title = Model.MetaTitle;
    Layout = "~/Views/Shared/Templates/_" + Model.Layout + "Layout.cshtml";

}
@Html.Raw(RazorEngine.Razor.Parse(Model.Markup, Model))

I have seen on the Codeplex site for RazorEngine the use of @Html (I know the version on there is out of date and I got my version via nuget).

Any help on this would be great.


The Html and Url helper properties are actual features of MVC's implementation of Razor in their view engine. Out of the box, Html and Url are not currently supported without specialising a custom base template.

The upcoming v3 release will be accompanied by an associated RazorEngine.Web release, which will hopefully include an MVC3 compatible base template with Html and Url support.

The example I wrote on the project homepage, is purely an example of using a custom base template.

You can find out more about v3 at https://github.com/Antaris/RazorEngine


Check https://github.com/Antaris/RazorEngine/wiki/6.-Encoding-Values page. I copy / past it here:

By default, RazorEngine is configured to encode as HTML. This sometimes presents problems where certain characters are encoded as HTML when you wanted the output to be as-is.

To output something in raw format, use the @Raw() built-in method as shown in the following example:

string template = "@Raw(Model.Data)";
var model = new { Data = "My raw double quotes appears here "hello!"" };

string result = Razor.Parse(template, model);

Which should result in:

My raw double quotes appears here "hello!"

It's quite old question but I found good answer on coderwall. The solution is to use:

@(new RawString("<strong>Bold!</strong>"))

or just:

@(new RawString(Model.YourHTMLStrinInModel))

I hope it's helpfull.

链接地址: http://www.djcxy.com/p/65090.html

上一篇: RazorEngine布局

下一篇: RazorEngine问题与@Html