The example in this article describes how asp.net implements the Razor template engine in non-MVC. Share to everyone for your reference. The specific analysis is as follows:
Template Engine Introduction
Razor, nvelocity, and Vtemplate,razor are generally used in MVC projects, and are described here for use in non-MVC projects.
How to use the Razor template engine in non-MVC
With open source Razorengine, we can use the razor engine in a ASP.net MVC project, and we can even use razor (own code builder) in the console and WinForm projects
How to use Razor
Environment Construction:
① Add Reference RazorEngine.dll
② Create cshtml
Create a new HTML and rename it to cshtml. Note: Open it by adding a--html page and changing it to a cshtml is there an automatic prompt, you must turn off the file and reopen it. Recommended, add--New Item--html page here directly to cshtml to create cshtml files, direct use of automatic prompts.
To start using:
1. Use razor syntax in cshtml
Razor, followed by an expression representing the value of an output expression at this position, model is the object passed to the template.
@{} for C # code, C # code can also be mixed with HTML code
2. Use of razor in general processing procedures:
the Razor object uses the parse method to parse the read cshtml into an assembly and then generate HTML.
public void ProcessRequest (HttpContext context)
{context
. Response.ContentType = "text/html";
String Fullpath=context. Server.MapPath (@ "~/razordemo/razor1.cshtml");
Get cshtml file path
string Cshtml=file.readalltext (FullPath);//Get file content
String html = Razor.parse (cshtml); Parse cshtml file parsing to get HTML context
. Response.Write (HTML);
3. How to read the value of an object in a cshtml file
Another overload of the Razor.parse () method is that it is passed into a model object, which can be used to point out the object's properties through model in the cshtml file.
Parsing in a generic handler:
Dog Dog = new Dog ();
Dog. Id = m;
Dog. Height =;
String html = Razor.parse (cshtml, dog);
Context. Response.Write (HTML);
To read object properties in cshtml:
I hope this article will help you with the ASP.net program design.