ASP. net mvc 2 custom view Engine

Source: Internet
Author: User

In ASP. in. Net MVC2, the view engine selects and creates view objects based on parameters. The view engine does not generate View content, and the View content is generated by the view object.
In ASP. NET MVC2, by default, the iviewengine interface must be implemented to create a custom view engine (this interface is declared in the system. Web. MVC namespace ). This interface declares three methods.CodeAs follows:

  1   Public     Interface  Iviewengine
2 {
3 Viewengineresult findpartialview (
4 Controllercontext,
5 String Partialviewname,
6 Bool Usecache );
7
8 Viewengineresult findview (
9 Controllercontext,
10 String Viewname,
11 String Mastername,
12 Bool Usecache );
13
14 Void Releaseview (
15 Controllercontext,
16 Iview view );
17 }
18  

Generally, almost all template engines use virtual paths to locate view files and load views. Therefore, if we use iviewengine, We need to write the corresponding path processing code.
To facilitate this general form, ASP. NET MVC2 provides a base class virtualpathproviderviewengine that encapsulates some path ProcessingProgram.
The virtualpathproviderviewengine class is an abstract class. This class contains query templates for various paths. You can query view files based on the controller and action values.
In addition, the virtualpathproviderviewengine class also contains two methods used to create a view. These two methods are image extraction methods. We need to implement these two methods when inheriting this class.
Using the virtualpathproviderviewengine class has another benefit: it automatically caches data in the production environment. We know that the View File is stored on the hard disk, and I/O operations on accessing the View File are very resource-intensive. Using the virtualpathproviderviewengine class solves this problem easily.
In general, the virtualpathproviderviewengine class requires the following class structure declarations:

  1   Public     Abstract    Class  Virtualpathproviderviewengine: iviewengine
2 {
3 Public String [] Masterlocationformats { Get ; Set ;}
4 Public String [] Partialviewlocationformats { Get ; Set ;}
5 Public String [] Viewlocationformats { Get ; Set ;}
6
7 Protected Abstract Iview createpartialview (
8 Controllercontext,
9 String Partialpath );
10
11 Protected Abstract Iview createview (
12 Controllercontext,
13 String Viewpath,
14 String Masterpath );
15 }
16  

That is to say, to inherit the virtualpathproviderviewengine class, we need to provide query templates for various View File paths and methods for creating views. In the View File Path template, {0} and {1} can be used to provide placeholders for the action name and controller name respectively.
From the structure of the virtualpathproviderviewengine class above, we can see that the two image extraction methods need to return an object of the class that implements the iview interface. Therefore, we need to implement a custom view object class that implements the iview interface.
The iview interface declaration is as follows:

  1 Public InterfaceIview
2 {
3 VoidRender (viewcontext, textwriter writer );
4 }

Here we only need to implement a render () method.
The render () method requires two parameters: viewcontext and writer. The function of this method is to generate the View content based on the data in viewcontext and write the generated content to the writer object.

After all the settings are completed, we need to register our custom view engine in global. the application_start () method in the asax file uses the engines set in the viewengines class to add our custom view engine. Of course, we can clear the default view engine of ASP. net mvc before adding it. The Code is as follows:

 
  1 Viewengines. Engines. Clear ();
2 Viewengines. Engines. Add (NewMyviewengine ());
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.