ASP. NET MVC-Customize your own Viewengine

Source: Internet
Author: User

http://blog.csdn.net/jackvs/article/details/7788743

When ASP. NET MVC came out so long, there was a lot of confusion in mind: Why do all the views have to be placed in the view directory? Why can the pages under the shared folder be shared? Why can the page be either *.cshtml or *.aspx?

In fact, some of the above problems are attributed to the effectiveness of the view engine.

In traditional ASP. NET, there may not be a concept of viewengine. Because the implementation page implements the Ihttphanlder interface in the Web from, the page is both the processing class for the response and the rendering class for the view. In ASP., the concept of the view is abstracted, and the concept of an attempt to engine is abstracted into an interface.

First look at the definition of the Iviewengine interface:

Namespace system.web.mvc{public    interface iviewengine    {        Viewengineresult findpartialview ( ControllerContext ControllerContext, String partialviewname, bool usecache);        Viewengineresult Findview (ControllerContext controllercontext, String viewName, String mastername, bool usecache);        void Releaseview (ControllerContext controllercontext, IView view);}    }


A total of 3 functions, summed up is probably two functions: Find & Release.


By default, ASP. NET MVC provides two view engines: Webformviewengine and Razorviewengine.

namespace system.web.mvc{public    static class Viewengines    {        private static readonly Viewenginecollection _ engines = new Viewenginecollection        {            new Webformviewengine (),            new Razorviewengine (),        };        public static viewenginecollection Engines        {            get {return _engines;}        }    }


This is why ASP. NET MVC supports both *.aspx and *.cshtml (individuals think it might be better to remove the webformviewengine if they have determined to use Razorview).

Then why should all the views be placed in the view directory, this will be given by Razorviewngines.

The following is the Razorviewengine constructor:

        Public Razorviewengine (Iviewpageactivator viewpageactivator): Base (Viewpageactivator) { Areaviewlocationformats = new[] {"~/areas/{2}/views/{1}/{0}.cshtml", "~/area S/{2}/views/{1}/{0}.vbhtml "," ~/areas/{2}/views/shared/{0}.cshtml "," ~/areas/{2}/views/share            D/{0}.vbhtml "}; Areamasterlocationformats = new[] {"~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/ {2}/views/{1}/{0}.vbhtml "," ~/areas/{2}/views/shared/{0}.cshtml "," ~/areas/{2}/views/shared/            {0}.vbhtml "}; Areapartialviewlocationformats = new[] {"~/areas/{2}/views/{1}/{0}.cshtml", "~/a Reas/{2}/views/{1}/{0}.vbhtml "," ~/areas/{2}/views/shared/{0}.cshtml "," ~/areas/{2}/views/sh            Ared/{0}.vbhtml "}; Viewlocationformats = new[]           {"~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.vbhtml", "~/views/sha            Red/{0}.cshtml "," ~/views/shared/{0}.vbhtml "}; Masterlocationformats = new[] {"~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.vbhtml            "," ~/views/shared/{0}.cshtml "," ~/views/shared/{0}.vbhtml "}; Partialviewlocationformats = new[] {"~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.v            bHTML "," ~/views/shared/{0}.cshtml "," ~/views/shared/{0}.vbhtml "};        Fileextensions = new[] {"cshtml", "vbhtml",}; }


All the addressing paths have been formatted, is it familiar, about why we use arrays instead of lists, and personally think that arrays are more efficient in addressing and traverse faster.

Well, find the "culprit", on the well-trained one, let it obediently obedient, sample let go where go.

Namespace mvcapplicatin.web.mvc.viewengine{public    sealed class Customviewengine:razorviewengine    {        Public Customviewengine ()        {            viewlocationformats = new[]            {                "~/views/{1}/{0}.cshtml",                "~/views/ Shared/{0}.cshtml ",                " ~/customviewlocation/{1}/{0}.cshtml "            };        }        public override Viewengineresult Findview (ControllerContext controllercontext, String viewName, string mastername, bool UseCache)        {            return base. Findview (ControllerContext, ViewName, Mastername, UseCache);}}}    


I use the development language is C #, the view is razor, so slightly modified the next viewlocationformats, so that the addressing path can find the customviewlocation below the view.

It's easy to go on, just empty the original view engine and load your own view engine.

        protected void Application_Start ()        {            arearegistration.registerallareas ();            ViewEngines.Engines.Clear ();            VIEWENGINES.ENGINES.ADD (New Customviewengine ());            Registerglobalfilters (globalfilters.filters);            RegisterRoutes (routetable.routes);        }




ASP. NET MVC-Customize your own Viewengine

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.