Brnshop open-source online store lecture 6: Extended view Function

Source: Internet
Author: User

Before officially explaining the extended view function, it is necessary to briefly describe the working principle of the view. Any view is translated into a C # class, saved to the specified position, and compiled. This is why the view contains C # code snippets. Next, let's take a look at this process through a specific project. First, we will create an mvc3 project. The code for its index. cshtml View File is as follows:

@ {Viewbag. title = "Homepage" ;}< H2> @ viewbag. message </H2> <p> for more information about ASP. for more information about net MVC, visit <a href = "http://asp.net/mvc" Title = "ASP. net MVC Website "> http://asp.net/mvc </a>. </P>

The view of the MVC application will not be compiled until the application starts. To view the classes translated from this view, you need to start the application and navigate to an action method. You can select any action method. This is because the initial request sent to the MVC application triggers the view compilation process. The class generated by the view is saved on the disk and then compiled. Take Windows 7 as an example. The file is saved in the c: \ windows \ Microsoft. NET \ framework \ v4.0.30319 \ temporary ASP. NET Files folder (for other versions of the operating system, see the corresponding folder ). Here I post the C # class code generated by the above view:

# Pragma checksum "C: \ Users \ Tony \ Desktop \ mvcapplication1 \ mvcapplication1 \ views \ home \ index. cshtml "" {ff1816ec-aa5e-4d10-87f7-6f4963833460} "" af52bbbcdf69be7d53364aceeb11ed8a27b4cdce "// generated // <auto-generated> // This code is generated by the tool. // Runtime version: 4.0.30319.18063 // changes to this file may result in incorrect behavior. If // re-generates code, these changes will be lost. // </Auto-generated> // -------------------------------------------------------------------------------- namespace ASP {using system; using system. collections. generic; using system. io; using system. LINQ; using system. net; using system. web; using system. web. helpers; using system. web. security; using system. web. ui; using system. web. webpages; using system. web. MVC; using system. web. MVC. ajax; using system. we B. MVC. HTML; using system. web. routing; public class _ page_views_home_index_cshtml: system. web. MVC. webviewpage <dynamic >{# line hidden public _ page_views_home_index_cshtml () {} protected ASP. global_asax applicationinstance {get {return (Asp. global_asax) (context. applicationinstance);} public override void execute () {# Line 1 "C: \ Users \ zhangmeng \ Desktop \ mvcapplication1 \ mvcapplication1 \ views \ H Ome \ index. cshtml "viewbag. title = "Homepage"; # line default # Line hiddenwriteliteral ("\ r \ n <H2>"); # Line 5 "C: \ Users \ zhangmeng \ Desktop \ mvcapplication1 \ mvcapplication1 \ views \ home \ index. cshtml "write (viewbag. message); # line default # Line hiddenwriteliteral ("</H2> \ r \ n <p> \ r \ n to learn about ASP. for more information about net MVC, visit <a href = \ "http://asp.net/mvc\" Title = \ "" + "ASP. net MVC website \ "> http://asp.net/mvc </a>. \ R \ n </P> \ r \ n ");}}}

The following are some notes about this file:

  1. Class Name format: the generated class name format is "_ page_views _ controller name_view name_file extension ". The generated class name "_ page_views_home_index_cshtml" is in this format.
  2. Parent class: if the view is not a strongly typed view, the class inherits from system. web. MVC. webviewpage <dynamic>. If the view is of a strong type, it is inherited from system. web. MVC. webviewpage <specified model class>.
  3. Output: The View content is output by calling the execute method of this class.

Now, you should have some knowledge about the working principle of the view (you can study this class more and it is helpful to improve the MVC level). Next, let's start with the question: extend the view function. As you can see from the working principle of the view described above, to expand the view function, a very good entry point is to extend the view base class. We take the multi-store online store brnmall as an example. The Code is as follows:

Using system; namespace brnmall. web. framework {// <summary> // base type of the front-end view page of the mall // </Summary> public abstract class webviewpage <tModel>: system. web. MVC. webviewpage <tModel> {public webworkcontext workcontext; Public override void inithelpers () {base. inithelpers (); workcontext = (basewebcontroller) (this. viewcontext. controller )). workcontext ;}//< summary> /// base type of the mall foreground view page /// </Summary> public abstract class webviewpage: webviewpage <dynamic> {}}

In this class, we add a property workcontext and assign values to it in the inithelpers method. In this way, we can directly access this attribute in the view without the need to convert the type through the Controller every time. The Code is as follows:

            @foreach (NavInfo info in WorkContext.NavList)            {            <li><a href="@info.Url">@info.Name</a></li>            }

However, to make this view base class work normally, you need to modify the Web. config file in the view folder. The Code is as follows:

<System. web. webpages. razor> 

Now, the content of today is over.

If you are interested in online mall programming, please join the QQ group: 235274151!

Brnshop open-source online store lecture 6: Extended view Function

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.