A MVC1 version of the Viewengine
From the above figure, we can know: (1) When the client sends the request, Defaultcontrollerfactory generates our controller according to the RequestContext object and Controllername. The controller viewdatadictionary will hold the relevant data and pass it through Viewengine to the view.
Public virtual IController Createcontroller (RequestContext
RequestContext, string controllername)
{
Type Controllertype = this. Getcontrollertype (RequestContext, controllername); Type = this
. Getcontrollertypewithinnamespaces (RequestContext.RouteData.Route, Controllername,
namespaces);
return this. GetControllerInstance (RequestContext, Controllertype);
}
Protected internal virtual IController getcontrollerinstance (RequestContext requestcontext, Type
controllertype)
{
return (IController) activator.createinstance (controllertype);//Reflection
}
(2) In our previous version of MVC, Controller owns Viewengine properties. Let's take a look at the following code: Viewengine How to handle view and respond to the client according to Viewcontext,viewlocator??
public void Renderview (ViewContext viewcontext)
{
string viewlocation = Viewlocator.getviewlocation ( ViewContext,
viewcontext.viewname)//Get the relative file path string ViewPath of view based on ViewContext and related content
ViewContext.HttpContext.Request.MapPath (viewlocation););//Get View
's absolute file path
string viewtemplate = File.readalltext (ViewPath)//Read the contents of the file
irenderer renderer = new Printrenderer ();
Viewtemplate = renderer. Render (Viewtemplate, viewcontext);//Regular expressions deal with the content, the
difficulty!
ViewContext.HttpContext.Response.Write (viewtemplate);//Send response to client
}
From the above code, we can see clearly that the overall framework is such a stark "request---> Response." Although the code is in the past version, it still makes it easier to understand the details of the process.
For custom viewengine of previous versions please refer to http://blog.maartenballiauw.be/post/2008/05/creating-a-custom-viewengine- For-the-aspnet-mvc-framework.aspx