Use Razor template engine and mvcrazor template engine from MVC

Source: Internet
Author: User

Use Razor template engine and mvcrazor template engine from MVC
About Razor template engine

 

1.Introduction

Template engine: Razor, Nveocity, and Vtemplate. Razor has an automatic VS prompt. It is easier to use.

However, Razor is mostly used in MVC.

So how to use Razor in a non-MVC environment?

2.Use Razor in non-MVC Mode

With the open-source RazorEngine, we can use the Razor engine in non-asp.net mvc projects.

You can even use Razor in winform projects (you can write code generators with high flexibility ).

 

3.Workflow

Through a big discussion with Yang zhongke, I roughly got the Razor workflow, as shown in. Overview of this blog

Based on this figure.

 

 

 

 

Try to use

1. Import DLL

 

2. Create cshtml

 

Create a cshtml file in a non-mvc project (the cshtml file can be prompted automatically)

The method is simple. Create an html file and change it to XX. cshtml.

In this way, our preparations are complete! Start playing.

 

3.Write a loop in cshtml using c # syntax 

In}, you can write C # code. C # code can be mixed with html code,

Writing like this can remind xiaobian of the years of doing php.

4.Read data from common processing programs

 

From the flowchart at the beginning, we know that we still need to use the Razor object to read the cshtml program.

Let him form an assembly and then generate html.

In fact, it is not very complicated to use it.

You can do this.

 

5) cshtmlHow to read values in an object

1) First, in a general processing program, an extra parameter is provided when the Parse method is called.

This parameter is the object you want to read in cshtml. (Theoretically, everything works)

2) Then we can find this object in cshtm.

@ Model. Age

@ Model. Name

You can write this code in cshtml and output it wherever you want.

 

End

So far, we have solved several basic problems about how to use the Razor template engine in a non-mvc environment.

 

1) how to create an environment. Import the dll to create a cshtml file.

2) embed C # code in the form. Effect of mixed editing

3) how to obtain the object Value in cshtml

This is basically the case.

 

 

Related Article

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.