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 objects in the view of the use of descriptive narrative. It's just not that the view length is not comprehensive, this article first explains the definition of the view engine and its use, thus slowly understanding the view engine and the working process of the View module.
ASP. NET MVC View
- Self-defined view engine simple Demo sample
- Razor View engine Run process
- Razor view-dependent injection, self-defined view helper
- segmentation, use of partial views
- Razor syntax, view helper
define the View engine yourself
Before explaining your own definition of the view engine. Let's first 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, which includes ViewData, viewbag some information, and so on. The number of references to the second string type is represented as the name of a partial view. The third parameter is the number of parameters of the Boolean type that indicates whether the current information is cached.
The Findview () method is similar to the Findpartialview () method, and only has a viewname number of references that represent the view name.
The actual implementation here is to return the iview "View processing type" of the corresponding view engine type in both methods based on the different view engine types. This part of the content will be explained in a short 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. The return type of the two methods above are viewengineresult types.
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 two constructors of the Viewengineresult type, the first enumerable string type representing such a collection of Search view location addresses. 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. // //number of references://ViewContext://the view context. // //Writer://The writer object. voidRender (ViewContext ViewContext, TextWriter writer); }
The iview type in my understanding is the view processing type, which represents only one type of view. Let's say razor view is a file in cshtml format. The corresponding iview is razorview this processing type, this next article will talk about.
Let's take a look at the demo examples below:
Figure 1
The approximate process is this. First, when our controller method returns Viewresult, Viewresult reads iviewengine from the Iviewengine collection of the system, and runs each iviewengine Findview "if it is a view", When a iviewengine in a run has a return viewengineresult type, it stops running down, but instead runs the render () method of iview 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 demo sample.
The first is the iviewengine of your own definition:
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. Assuming that the view name is called "Mycustomview", the view processing type of the Mycustomview type is returned as a constructor parameter of the Viewengineresult type.
Self-defined 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 simply to write the value of ViewData to writer, and finally on the current view page.
Finally, we will add our own defined 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 ());
The way to join is not much to say, the previous space for such a pattern has been said, so that their definition of the first place in the collection.
The last arbitrary code change in a controller method is for example 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 co-owned, welcome reprint, but without the author's permission must retain this paragraph, and on the article page
ASP. NET MVC view (i)