Page base class differences between ASP. WebForm and Razor

Source: Internet
Author: User

It is known that the base class for page pages is declared in the traditional WebForm mode:

<%@ page language="C #" masterpagefile= "~/views/shared/site.master " inherits="viewpage" %>
View Code

In the case of partial view, this is stated:

<%@ Control language="C #" inherits= "viewusercontrol<dynamic>  " %>
View Code

You can know that if the traditional view is inherited from the ViewPage class, if it is Partialview, then it inherits from the Viewusercontrol class.

In order to prove that our analysis is correct, we look at the source of ASP. NET MVC, there is a Webformview class in the source code, this is the base class that supports our traditional Web form, let's take a look at how its renderview is implemented, by the way, Webformview inherits from Buildmanagercompiledview, which we discuss later.

protected Override voidRenderview (ViewContext ViewContext, TextWriter writer,Objectinstance) {ViewPage ViewPage= Instance asViewPage; if(ViewPage! =NULL) {renderviewpage (ViewContext, viewpage); return; } Viewusercontrol Viewusercontrol= Instance asViewusercontrol; if(Viewusercontrol! =NULL) {Renderviewusercontrol (ViewContext, Viewusercontrol); return; }            Throw NewInvalidOperationException (String.Format (CultureInfo.CurrentCulture,        Mvcresources.webformviewengine_wrongviewbase, ViewPath)); }
View Code

We can clearly see that the object instance first to determine whether it is a viewpage type, if NULL to determine whether it is a viewusercontrol type, if neither is thrown exception, so we can know that The traditional WebForm mainly consists of these two base classes. In the case of the traditional ASP. NET WebForm, you need to prove it yourself.

Well, we've analyzed ASP. WebForm, and if you want to rewrite the base class later, just rewrite the viewpage and Viewusercontrol as needed.

Since the introduction of a new view engine razor after ASP. NET MVC 3.0, has the base class structure changed?

The fact is that when we use the razor engine, the. cshtml file we created did not have a header statement similar to WebForm before:

<%@ page language="C #" masterpagefile= "~/views/shared/site.master " inherits="viewpage" %>
View Code

Instead of nothing, this moment let us need to rewrite the base class of children's shoes can not touch the mind, well, let us use the same way of analysis, Razor's head statement does not mean that he does not inherit the class, Since the omission then explains a very straightforward reason is that their view and Partialview's head statement should be the same, so Microsoft simply omitted to write. In order to demonstrate whether our conjecture is correct, let's look at the source code.

Also in the source code has a Razorview class, this and Webformview is corresponding, he also inherits from Buildmanagercompiledview class, similarly let us see how his Renderview method is implemented.

protected Override voidRenderview (ViewContext ViewContext, TextWriter writer,Objectinstance) {            if(writer = =NULL)            {                Throw NewArgumentNullException ("writer"); } webviewpage webviewpage= Instance asWebviewpage; if(Webviewpage = =NULL)            {                Throw NewInvalidOperationException (String.Format (CultureInfo.CurrentCulture,            Mvcresources.cshtmlview_wrongviewbase, ViewPath)); }            //An overriden master layout might has been specified when the Viewactionresult got returned. //We need to the it have executed for it so, we can set it on the inner page once.Webviewpage.overridenlayoutpath =Layoutpath; Webviewpage.virtualpath=ViewPath; Webviewpage.viewcontext=ViewContext; Webviewpage.viewdata=Viewcontext.viewdata;            Webviewpage.inithelpers (); if(Virtualpathfactory! =NULL) {webviewpage.virtualpathfactory=virtualpathfactory; }            if(Displaymodeprovider! =NULL) {Webviewpage.displaymodeprovider=Displaymodeprovider; } webpagerenderingbase startpage=NULL; if(runviewstartpages) {startpage=startpagelookup (Webviewpage, Razorviewengine.viewstartfilename, viewstartfileextensions); } webviewpage.executepagehierarchy (NewWebpagecontext (Context:viewContext.HttpContext, page:NULL, Model:NULL), writer, startpage); }
View Code

Just as we suspect that only one Webviewpage class is seen in the Renderview method, then the Razor engine's view and Partialview base classes are merged, but we can also tell by extension Because the razor extension, both view and Partialview, is. cshtml, and the previous WebForm mode is different.

Now that Razor has been merged, the head statement is certainly the same, so Microsoft has omitted this link.

So razor how to rewrite the base class, if you declare the page header inheritance relationship?

We also inherit from the Webviewpage base class and then make the following statement on the head of our view or Partialview:

@inherits Customwebviewpage

Or

@inherits customwebviewpage<custommodel>

This enables the implementation of the same custom implementation, but here is a point, because Webviewpage is an abstract type of class, so can not be directly instantiated, then a bit of inheritance, there must be a way to implement a Excute method. If the customwebviewpage you declare is also an abstract type, you do not need to rewrite it, call the default implementation, but if not, implement the custom Excute method yourself.

Page base class differences between ASP. WebForm and Razor

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.