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 use .NET MVC Razor

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Net MVC Razor how to use, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Busy work always takes up most of the time of life! So my blog up to now is still a few articles, technology is used to share and learn, there are different views on technology, everyone can share, if there are any problems in the following article, please point out, in this free time to tell you that mvc Razor in addition to parsing in the view, can also be used.

In some project requirements may need to produce static pages according to the template, then you can also use Razor syntax to directly parse your page to generate a static page, first introduce RazorEngine, in nuget can be directly referenced to the project, this thing is popular that you can parse razor syntax anywhere, I think it is much more flexible and easy to use than NVelocity. There is an introduction on codeplex, but now it seems to have moved to Github. The link to the version of http://razorengine.codeplex.com/ is available on Github. It is very convenient to use, as shown below:

String template = "Hello @ Model.Name! Welcome to Razor!"; string result = Razor.Parse (template, new {Name = "World"})

As simple as that, you can not only use Razor on the view page, it fully supports dynamic typing, as follows:

Dynamic ViewBag = new DynamicViewBag (); ViewBag.list = "fleeting years"; string template = "Hello Word @ ViewBag.list"; string result = Razor.Parse (template,null,ViewBag,Guid.NewGuid (). ToString ())

It can also be parsed. If you see that the source code is alive is the intelligent prompt of VS, you can see the parameter types that need to be passed behind Razor.Parse. As long as you are seated, it supports cache. Of course, what I personally understand about this kind of cache is not the kind of data cache cache, such as the above dynamic type parsing Razor.Parse. I am using Guid for demonstration, but you can use other string characters. If you still use the same name when you precompile the template next time, you will directly use the template in cache to do parsing, so as to reduce the parsing time. This is my personal understanding, and I haven't studied it in depth yet.

RazorEngine also supports custom templates, such as the following code:

/ / Custom template public class MyTemplateBase: TemplateBase {public string GetStr () {return "fleeting time";}} / / Register Custom template public class CustomTemplate: TemplateService {public CustomTemplate () {var Service = new RazorEngine.Configuration.TemplateServiceConfiguration (); Service.BaseTemplateType = typeof (MyTemplateBase) Var MyTemplate = new TemplateService (Service); Razor.SetTemplateService (MyTemplate);}}

A brief introduction, ah, the above is a custom template, in which you can write the corresponding method according to your needs, so you can also use your method when parsing, for example, mine is the returned string, you can also get your return value with @ GetStr (). In addition, your custom template also inherits TemplateBase. I read its source code and don't know why I should bring a generic type. If you want to study, you can see for yourself. After registering the template, you can use it.

CustomTemplate service = new CustomTemplate (); return service.Parse (Content, null, ViewBag, Guid.NewGuid (). ToString ())

According to this method can be parsed, the Razor.Parse method is a virtual method in the TemplateService class, is to support their own rewriting, so when you register when you inherit the TemplateService class, you can achieve what you want!

See that there are a lot of questions on the official website about why you can't use @ Html.Raw () and other methods. In fact, you can support it if you look at the source code carefully, but if you change the writing method, you can directly use @ Raw () for output. This is a brief introduction. In fact, RazorEngine has many functions, but at present, the above are enough to parse the page!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 289

*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