MVC 3 allows you to create your own view engine without using the built-in razor view or apsx view. The following describes how to create a custom view engine.
First, view engine inherits from the iviewengine interface. The iviewengine interface is defined as follows:
1 Public Interface Iviewengine { 2 Viewengineresult findview (controllercontext, String Viewname, 3 String Mastername, Bool Usecache ); 4 Viewengineresult findpartialview (controllercontext, 5 String Partialviewname, Bool Usecache ); 6 Void Releaseview (controllercontext, iview view ); 7 }
The role of the view engine is to convert the request to viewengineresult. the parameter of the first and second methods requires a controllercontext, A viewname, A mastername and a parameter that specifies whether previous results can be reused in the cache. The last method releaseview is called when the view is not used and will be released.
The viewengineresult class allows the view engine to respond to a request when the view needs to be displayed. You can create viewengineresult using one of the following constructor types:
1. Public viewengineresult (iview view, iviewengine viewengine );
The constructor parameters are inherited from the iview interface and view engine.
If your view engine does not provide a view that responds to a request, you can use the following constructor:
2. Public viewengineresult (ienumberable <string> searchlocations );
The constructor of this version looks for view files in several places. If the view files cannot be found, an error message is displayed.
The last block in the view engine system is the iview interface, as shown in the following figure.CodeAs shown in:
1 NamespaceSystem. Web. MVC {2 UsingSystem. IO;3 Public InterfaceIview {4 VoidRender (viewcontext, textwriter writer );5 }6}
We pass an iview object to the constructor of the viewengineresult object, and then return the view engine method. The MVC Framework calls the render method. The viewcontext context contains the request information from the client. textwriter will output some content to the client.
The best way to learn the relationship between iviewengine, iview, and viewengineresult is to create a view engine.
Next articleArticle, We will create a view engine of our own.