ASP. NET MVC view (i)
Preface
From the beginning of this article into the view part of MVC, in some of the previous space more or less views and some of the objects in the view of the use of the description, but after all, not the view length is not comprehensive, this article first to explain the definition and use of the custom view engine, This slowly takes you through the process of working with the view engine and the View module.
ASP. NET MVC View
- A simple example of a custom view engine
- Razor view Engine Execution process
- Dependency Injection, Custom view helper for razor views
- segmentation, use of partial views
- Razor syntax, view helper
Custom View engine
Before we go into the custom view engine, let's look at some of the object types involved.
First look at the definition of the Iviewengine interface type:
Code 1-1
Public Interface Iviewengine { stringbool usecache); string string BOOL usecache); void Releaseview (ControllerContext controllercontext, IView view); }
In code 1-1 we can see that there are three methods defined in the Iviewengine interface type, the first parameter in the first Findpartialview () method is the controller context type, the ViewData, viewbag some information, etc. The second parameter of the string type is represented as the name of the partial view, and the third parameter is a Boolean-type parameter that indicates whether the current information is cached.
The Findview () method is similar to the Findpartialview () method, except that it has a viewname parameter that represents the view name.
The actual implementation here is based on the different view engine types in these two methods to return the corresponding view engine type of the iview "View processing Type", this part of the content will be explained in a few space.
The Releaseview () method is used to release the resource that handles the view in iview.
The Viewengineresult type is an operation return type that encapsulates the Iviewengine type and iview type, and the return type of the two methods above is the Viewengineresult type.
Code 1-2
public class viewengineresult{ public Viewengineresult ( Ienumerable<string > searchedlocations); public Viewengineresult (IView view, Iviewengine viewengine); public ienumerable<string > searchedlocations {get ;} public IView View {get ; public iviewengine viewengine {get Span style= "COLOR: #000000" >; } }
In code 1-2 we can see the viewengineresult type of two constructors, the first enumerable string type represents a search view location of the address of such a collection, the second needless to say is the object encapsulation.
Let's take a look at the definition of iview:
Code 1-3
Public InterfaceIView {//Summary://renders the specified view context using the specified writer object. // //Parameters://ViewContext://the view context. // //Writer://The writer object. voidRender (ViewContext ViewContext, TextWriter writer); }
iview type in my understanding is the view processing type, it just represents a type of view, such as Razor view is cshtml format of the file, the corresponding iview is razorview this processing type, this next article will talk about.
Let's take a look at the following examples:
Figure 1
The process is like this, first, when our controller method returns Viewresult, Viewresult reads iviewengine from the Iviewengine collection of the system and executes iviewengine for each Findview " In the case of a view, the execution of a iviewengine with a return viewengineresult type will stop execution and execute the iview render () method in the Viewengineresult type. The rendering of the final view is not part of the MVC section, which is explained in the next article. Now let's take a look at the example.
The first is the custom iviewengine:
Code 1-4
usingSYSTEM.WEB.MVC;usingMvcapplication.customview;namespacemvcapplication.customviewengine{ Public classMycustomviewengine:iviewengine { PublicViewengineresult Findpartialview (ControllerContext controllercontext,stringPartialviewname,BOOLUseCache) { return NewViewengineresult (New string[] {"Mycustomview" }); } PublicViewengineresult Findview (ControllerContext controllercontext,stringViewName,stringMastername,BOOLUseCache) { if(ViewName = ="Mycustomview") { return NewViewengineresult (NewMycustomview (), This); } Else { return NewViewengineresult (New string[] {"Mycustomview" }); } } Public voidReleaseview (ControllerContext controllercontext, IView view) {}}}
This is just a basic code implemented in the Findview () method, and if the view name is called "Mycustomview", the view processing type of the Mycustomview type is returned as a constructor parameter of the Viewengineresult type.
Custom iview:
Code 1-5
usingSYSTEM.WEB.MVC;namespacemvcapplication.customview{ Public classMycustomview:iview { Public voidRender (ViewContext ViewContext, System.IO.TextWriter writer) {foreach(stringKeyinchViewContext.ViewData.Keys) {writer. Write ("Key:"+ key +", Value:"+ Viewcontext.viewdata[key] +".<p/>"); } } }}
The definition in code 1-5 is a simple write-ViewData value to writer, and is finally rendered on the view page.
Finally, we will add the custom view engine to the system's view engine collection, in the Application_Start () method of the Global.asax file.
Code 1-6
ViewEngines.Engines.Insert (0new customviewengine.mycustomviewengine ());
This way of adding is not much to say, the previous space for this model has been said, let the custom row in the first place in the collection.
Finally, the code in a controller method is changed to the following code:
Code 1-7
PublicActionResult CustomView () { This. Viewdata.add ("Debugdata","Jinyuan"); This. Viewdata.add ("debugdate","2014-01-01"); returnView ("Mycustomview"); }
Figure 2
Jinyuan
Source: http://blog.csdn.net/jinyuan0829
This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page
ASP. NET MVC view (i)