Into the razor template engine in ASP.net MVC 3.0

Source: Internet
Author: User
Tags add min net

With the release of the Mvc3.0rtm release, the company's projects were recently upgraded from MVC2.0 to MVC3.0. The razor template engine is also intended to be fully used in MVC3. Now will razor learn to take out and everyone to share, if there are insufficient places to welcome you to point out.

In fact, when using <%=%> to invoke C # code in HTML, the heart is always complaining. This is a very troublesome formulation. Where's The trouble? is actually closed. Like what:

asp.net: <script src= "<%=url.content (" ~/scripts/jquery-1.4.4.min.js ")%>"

Razor: <script src= "@Url. Content (" ~/scripts/jquery-1.4.4.min.js ")"

Obviously, razor in the interior to help us do a closed "%>". In fact, this little closure allows us to invoke the server-side code more "smoothly" within the HTML. Therefore razor to the development has brought certain convenience! The basic usage of razor is described below.

One, template page

Once razor appears, we can choose not to use the ASP.net Master template page. Instead, the template file for Cshtml Razor. The use of individuals is considered to be similar to the Master template page. But in the original function of Mater template page has been further expanded, more convenient development. For example, if you add a _viewstart.cshtml file to the View folder, we don't need to introduce a template page to each specific view page. Reduce the duplicate code within the view page. Specifically, you can create a MVC3 application Select Razor template, vs will automatically create the above mechanism. What is worth proposing here is the @rendersection method. Lets you preset an area on the template page and use it in the future for the view that inherits the template page. See the following actions:

Under the MVC3.0 shared folder, add the following code _layout.cshtml the template file:

 
  
  
  1. <! doctype html>   
  2.  
  3.  
  4.    <title> @ViewBag .title</title>   
  5.   <link href= @Url. Content ("~/content/site.css") " rel=" stylesheet " type=" Text/css " />   
  6.    <script src=" @Url. Content ("~/scripts/ Jquery-1.4.4.min.js ")"  type= "Text/javascript" ></script>   
  7.     @RenderSection ("Head",  required: true)    
  8.  
  9.  <body>   
  10.   <div class= "page" >   

The above code, line 7th, reserves the section of a head. People who are familiar with the template page should understand that the reservation is a custom specific js/css used for future specific view pages. We will use the default home index to illustrate this view. Open index.cshtml write the following code

 
  
  
  1. @{
  2. Viewbag.title = "Home Page";
  3. }
  4. H2> @ViewBag .message
  5. @section head{
  6. <script type= "Text/javascript" >
  7. $ (function () {
  8. Alert ("Hello jquery");
  9. });
  10. </script>
  11. }

The view has been integrated into the template page, and this is just filling in the head section within the template page and then you can add JS or CSS for each view page. So far to achieve the characteristics of the personality outside.

Second, Razor grammar:

At the beginning of the article, it has been mentioned that the convenience of razor grammar is that Razor automatically helps us to close the syntax of C # or vb.net in HTML. Please see the following code:

has been given a note, it is not difficult to read carefully. You should also be able to realize that if you replace razor with ASP.net's <%=%> or <%:%>, it is really "painful". As for the other uses of razor, the official website has been written in a comprehensive way, such as Razor's HTML encod prevent XSS attacks, text in the HTML string that appears xxx@xxx.com, Razor is a keyword that can be automatically identified as an email format rather than a razor.

Third, Razor syntax smart tips:

The smart tip is the same as in VS, with just Ctrol + J to pull out. Please take a look at the picture below

Do you want to use razor in web development as much as I do?

Original link: http://www.cnblogs.com/ryanding/archive/2011/01/19/1938690.html

"Edit Recommendation"



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.