MVC Multi-level directory (Controller) routing rewrite and multi-level views directory for view-seeking rules

Source: Internet
Author: User

Turn from: [Original]asp.net MVC Multi-level controller routing rewrite and multi-level views directory to find the rules of view

In order to better control views of the page storage, and controller readability, need to separate multi-level directory to store.

1. Then we'll look at the way we need access, such as

If we want to access the admin under the TestController Index page, then we enter the Test/index, this is definitely not. Because TestController is not at all in the root directory of controllers, but under the controllers/admin, so we can not find the controller of test. Then we enter Admin/test/index, then we need to add a route configuration, because the previous default route can only be accessed through {Controller}/{action}/{id}, that is, it must start with a Controller. The routes we reconfigure are as follows:

 Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); //routing rule matching is from top to bottom, and priority matching routes must be written at the top. Since the route match succeeds, he will not continue to match. routes. MapRoute ("Admin",//The name of the route, as long as it is guaranteed to be unique in the routing set                "Admin/{controller}/{action}/{id}",//routing rules, matching URLs beginning with admin                New{controller ="Home", action ="Index", id = urlparameter.optional}//             ); Routes. MapRoute ("Default",//Route name                 "{Controller}/{action}/{id}",//URL with Parameters                 New{controller ="Home", action ="Index", id = urlparameter.optional}//Parameter Defaults             ); }

Then we enter Admin/test/index again this time, can find views/admin/test/index.cshtml this page? Obviously not, because in addition to the routing configuration how to access the controller, the search for views inside the page also has its own rules. The test result is definitely not find the page, we look at the error message to know how he looked for the cshtml page.

    

The address of the Razor view, written as a wildcard character, is:
  • Views/{1}/{0}.cshtml
  • Views/shared/{0}.cshtml

{1} represents the name of the controller, {0} represents the view name, and shared is the folder where the template page is stored. It's clear at first sight. This is the rule of looking for a view, so the storage rules we store in admin/test/index.cshtml are not satisfied. Then we modify the following, such as:

Put the Test folder directly under the views, then we will satisfy the rule of looking for the view, we entered Admin/test/index, and indeed the visit was successful.

    

However, the storage of this method is certainly not what we need, since our controller distinguishes the storage, we certainly hope that views can also be stored in this way.

3. Then we go to the point and modify his view-seeking rules so that he can access them according to our rules, just like modifying routes.

Create a new CS class myviewengine in the project and inherit the Razorviewengine. The code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcroute.mvcex{ Public Sealed classMyviewengine:razorviewengine { PublicMyviewengine () {viewlocationformats=New[]            {                "~/views/{1}/{0}.cshtml",                "~/views/shared/{0}.cshtml",                "~/views/admin/{1}/{0}.cshtml"//our Rules.            }; }         Public OverrideViewengineresult Findview (ControllerContext controllercontext,stringViewName,stringMastername,BOOLUseCache) {            return Base.        Findview (ControllerContext, ViewName, Mastername, UseCache); }    }}

Then register this rule in the system and register it in global so that we can access it in our own way. Global registration is as follows:

protected void Application_Start ()        {            arearegistration.registerallareas ();            Registerglobalfilters (globalfilters.filters);            RegisterRoutes (routetable.routes);            Registerview (); // registering view access rules         }        protectedvoid  Registerview ()        {            ViewEngines.Engines.Clear ();            VIEWENGINES.ENGINES.ADD (new  myviewengine ());        }

Results such as:

    

MVC Multi-level directory (Controller) routing rewrite and multi-level views directory for view-seeking rules

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.