asp.net practical tips for implementing the Razor template engine in non-MVC

Source: Internet
Author: User

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

<! DOCTYPE html>
 
 

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:

<! DOCTYPE html>
 
 

I hope this article will help you with the ASP.net program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.