ASP. NET MVC view (i)

Source: Internet
Author: User

ASP . NET MVC View ( i )

Objective

from the beginning of this article to enter the The view part of MVC, in some of the previous space, is more or less the use of some objects in view and views, but after all, is 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.netmvc View

l A Simple example of a custom view engine

l Razor View Engine Execution procedure

l Razor Dependency Injection, Custom view helper for views

l segmentation, use of partial views

l 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 . Iviewengine definition of the interface type:

Code 1-1

public interface iviewengine    {         viewengineresultfindpartialview (Controllercontextcontrollercontext,  string partialviewname, bool usecache);         Viewengineresultfindview (controllercontextcontrollercontext, string viewname, string  Mastername, boolusecache);         voidreleaseview ( Controllercontextcontrollercontext, iview view);     } 

in code 1-1 We can see that there are three methods defined in the Iviewengine interface type, and the first parameter in the first Findpartialview () method is the controller context type. The bread is ViewData,ViewBag Some information, and so on, 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 to return the corresponding view engine type in both methods according to the different view engine types. IView"View processing Type", this section will be explained in a few pages.


The Releaseview () method is used to release the resource that handles the view in IView.

Viewengineresult type is encapsulated with Iviewengine Types and IView an operation return type of type, the return type of the two methods above is Viewengineresult type.

Code 1-2

public class viewengineresult{Publicviewengineresult (ienumerable<string> searchedlocations);         Publicviewengineresult (IView view, Iviewengine viewengine);        Public ienumerable<string>searchedlocations {get;}        Public IView View {get;}    Public Iviewengine viewengine {get;} }

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 see it again . IView definition of:

Code 1-3

Public interface IView {//Summary://Use the specified writer object to render the specified view context.        Parameters://ViewContext://view context.        Writer://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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3F/A5/wKiom1PLq5ryzzfFAAG-ySaBFVo789.jpg "title=" Viewengine1.png "width=" 738 "height=" 239 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:738PX;HEIGHT:239PX; "alt=" Wkiom1plq5ryzzffaag-ysabfvo789.jpg "/>

probably the process is like this, first in our controller method to returnViewResultthe time,ViewResultwill be from the system'sIviewengineread in the collectionIviewengine, and perform eachIviewengineof theFindview"If it is a view", perform one of theIviewenginehave returnedViewengineresulttype, it will stop and executeViewengineresultin the typeIViewof theRender ()method. The rendering of the last view is notMVCpart of the responsibility of the next article will be explained. Now let's take a look at the example.

The first is the custom Iviewengine :

Code 1-4

using system.web.mvc;using mvcapplication.customview; namespace  mvcapplication.customviewengine{    public class mycustomviewengine:  iviewengine    {         public  Viewengineresult findpartialview (controllercontext controllercontext, string  Partialviewname, boolusecache)         {             returnnew viewengineresult (new string[] {  "mycustomview " &NBSP;});        }          public viewengineresult findview (controllercontextcontrollercontext,  String viewname, string mastername, boolusecache)          {          &Nbsp; if (viewname ==  "Mycustomview")              {                 returnnew viewengineresult (New mycustomview (), this);             }            else             {                 returnnew viewengineresult (new string[]  {  "mycustomview " &NBSP;});             }         }          public void releaseview (Controllercontextcontrollercontext, iview view)          {         }    }} 

This is just a The Findview () method implements a base code that, if the view name is called "Mycustomview", returns mycustomview The view processing type of the type is returned as a constructor parameter of the Viewengineresult type.

Custom-Defined IView:

Code 1-5

using system.web.mvc; namespace mvcapplication.customview{     public class MyCustomView:IView    {         public void render (Viewcontextviewcontext, system.io.textwriter  writer)         {             foreach (String key inviewcontext.viewdata.keys)              {                 writer. Write ("Key:"  + key +  ", Value:" + viewcontext.viewdata[key] +  ".<p/>");             }         }    }} 

The definition in code 1-5 is to simply write The value of ViewData to writer and finally render it on the view page.

Finally, we will add the custom view engine to the system's view engine collection, Global.asax file in the Application_Start () method.

Code 1-6

ViewEngines.Engines.Insert (0, Newcustomviewengine.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

Public Actionresultcustomview () {this.            Viewdata.add ("Debugdata", "Jinyuan"); This.                     Viewdata.add ("Debugdate", "2014-01-01");        Returnview ("Mycustomview"); }

Figure 2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3F/A5/wKiom1PLq-CiYpRvAADU2IMK_UA436.jpg "title=" Viewengine2.png "width=" 738 "height=" 184 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:184px; "alt=" Wkiom1plq-ciyprvaadu2imk_ua436.jpg "/>


This article is from the "Jinyuan" blog, please be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1440579

ASP. NET MVC view (i)

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.