Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement asp.net using Razor template engine in non-MVC

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve asp.net in non-MVC using Razor template engine", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to achieve asp.net in non-MVC in the use of Razor template engine" bar!

The specific analysis is as follows:

Introduction to template engine

Razor, Nvelocity, and Vtemplate,Razor are generally used in MVC projects, which are described here in non-MVC projects.

How to use Razor template engine in non-MVC

With the open source RazorEngine, we can use the Razor engine in non-asp.net mvc projects, and even in the console and WinForm projects, we can use Razor (develop our own code generator)

How to use Razor

Environment building:

① add reference RazorEngine.dll

② creates cshtml

Create a new html and change its name to cshtml. Note: there is no automatic prompt to open it by adding-- html page and then changing it to cshtml. You must close the file and reopen it. Recommended to use, add-- New item-- html page here directly changed to cshtml to create cshtml files, directly available for automatic prompt.

Get started with:

1. Using Razor syntax in cshtml

The @ in Razor followed by the expression indicates that the value of the expression is output at this location, and the Model in the template is the object passed to the template.

@ {} is the C # code, and the C # code can also be mixed with the html code

@ {for (int I = 0; I < 10; iTunes +) {@ I}}

two。 Use Razor in general handlers:

The Razor object parses the read cshtml into an assembly using the Parse method, and then generates the html.

Public void ProcessRequest (HttpContext context) {context.Response.ContentType = "text/html"; string fullPath=context.Server.MapPath (@ "~ / Razordemo/Razor1.cshtml"); / / get the cshtml file path string cshtml=File.ReadAllText (fullPath); / / get the file content string html= Razor.Parse (cshtml); / / parse the cshtml file 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 to pass in a Model object whose properties can be pointed out through Model in the cshtml file.

Parse in a general handler:

Dog dog = new Dog (); dog.Id = 100th dog.Height = 120X string html = Razor.Parse (cshtml, dog); context.Response.Write (html)

Read object properties in cshtml:

Dog Information: Id:@Model.Id height: @ Model.Height Thank you for your reading, this is the content of "how to achieve asp.net using Razor template engine in non-MVC". After the study of this article, I believe you have a deeper understanding of how to achieve asp.net in non-MVC using Razor template engine, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report