How is the view of ASP.net mvc presented? [Design Article]

Source: Internet
Author: User
Tags definition bool instance method

In the previous four articles, we introduced various ActionResult and related request-response mechanisms, but the actionresult associated with "view rendering" is Viewresult. The rendering of view through Viewresult implementation is much more complicated than the various actionresult we have described above, and ASP.net MVC internally designed an extended view engine that implements the final view rendering work. [This article has been synchronized to ' how ASP.net MVC works ']

View in the View engine

asp.net MVC provides us with two view engines, which are designed for different dynamic view styles. One is the traditional Web Form engine, because the design of view under the engine is consistent with our definition of. aspx page, also known as the ASPX engine. The other is this book by default and also recommended for use by the razor engine. Before the two view engines work, there's a question you have to know: How does view represent? When it comes to view, many asp.net MVC developers may first think of the. aspx file (Web form engine) or the. cshtml/.vbhtml file (the Razor engine) that defines the UI interface. In fact, for the view engine, view is an object that implements the IView interface type. As the following code fragment shows, the definition of iview is very simple, with only a unique render method to render the view based on the specified view context and TextWriter object.

 1:public Interface IView 
2: {
3:void Render (ViewContext ViewContext, TextWriter writer);
4:}
5: 
6:public class Viewcontext:controllercontext
7: {
8://other member
9:pub LIC virtual bool clientvalidationenabled {get; set;}
10:public virtual bool unobtrusivejavascriptenabled {get; set;}
11: 
12:public virtual tempdatadictionary tempdata {get; set;}
: [Dynamic]
14:public object ViewBag {[return:dynamic] get;}
15:public Virtual viewdatadictionary ViewData {get; set;}
16:public Virtual IView View {get; set;}
17:public Virtual TextWriter Writer {get; set;}
18:}
19:&NBSP
20:public abstract class Httpresponsebase
: {
22://Other Members
23:public virtual TextWriter Output {get; set;}
:}

IView the Render method used to render view has two parameters and one is the ViewContext object that represents the view context. The above code fragment shows that ViewContext is a subclass of ControllerContext, which represents the ViewData, ViewBag, and tempdata of state data corresponding to the Controllerbase's attribute of the same name. That is, when performing a Viewresult returned from a controller action method, the state data persisted through the created ViewContext is directly derived from the Controller object.

ViewContext has two Boolean-type properties clientvalidationenabled and unobtrusivejavascriptenabled that indicate whether client-side validation and unobtrusive JavaScript are supported. By default, two properties are set by using the appsettings configuration item with the same name. If the application does not have a corresponding configuration, the default value of two properties is false.

   1: <configuration>
2: <appSettings>
3: <add key= "clientvalidationenabled" value= "true"/>
4: <add key= "unobtrusivejavascriptenabled" value= "true"/>
5: </appSettings>
6: </configuration>

The scope of the configuration is for the entire Web application, and this global property can also be set through HtmlHelper static properties of the same name. It is worth mentioning that ASP.net MVC allows us to turn on or off support for client-side validation and unobtrusivejavascriptenabled for a view. This can be achieved through the HtmlHelper instance method Enableclientvalidation/enableunobtrusivejavascript of the current view.

   1:public class HtmlHelper
2: {
3: //other Members
4: Public void Enableclientvalidation ();
5: Public void Enableclientvalidation (bool enabled);
6: Public void Enableunobtrusivejavascript ();
7: Public void Enableunobtrusivejavascript (bool enabled);
8:
9: Public static bool clientvalidationenabled {get; set;}
Public static bool unobtrusivejavascriptenabled {get; set;}
11:}

The second parameter of the Render method of the interface iview is a TextWriter object. For this method, as soon as we write the content to the TextWriter, we complete the rendering of the relevant content on view, because when the Render method is invoked, it is the TextWriter represented by the current HttpResponse output property.

Related Article

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.