Love mvc~ in the multi-level folder of views

Source: Internet
Author: User

Back to Catalog

In the MVC, your controller corresponds to the view is generally in the views directory, and if you want to be divided into a few modules folder by default is not allowed, we need to do a set, you can achieve the multiple folder hierarchy under Views, for example, we have product modules, user modules, order module, We can design the folder structure as follows

User module:/views/usermodel/usercenter/index, where Usermodel is the module name, Usercenter is the controller name, and Index is the action name

Product module:/views/productmodel/product/index

Order module:/views/ordermodel/order/do

If we want to implement a view of this way, we need to configure the following code

1 Build your route

     /// <summary>    ///Background Routing/// </summary>     Public classAdmin_routing:razorviewengine { Publicadmin_routing () {viewlocationformats=New[]            {              "~/views/{1}/{0}.cshtml",                "~/views/admin/{1}/{0}.cshtml"//Customizing the car sales attempt            }; }         Public OverrideViewengineresult Findview (ControllerContext controllercontext,stringViewName,stringMastername,BOOLUseCache) {            return Base.        Findview (ControllerContext, ViewName, Mastername, UseCache); }    }

2 Configure your route, you can write down the default route below

   Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); 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}",                New{controller ="WebUser", action ="Index", id = urlparameter.optional}//            ); }    }

3 Add a view engine to global and add a custom route

protected void registerview_custom_routing ()        {            ViewEngines.Engines.Clear ();            VIEWENGINES.ENGINES.ADD (new  admin_routing ());        }
  protected void Application_Start ()        {            arearegistration.registerallareas ();            Webapiconfig.register (globalconfiguration.configuration);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (bundletable.bundles);            Authconfig.registerauth ();            Registerview_custom_routing ();        }

Finally, run our Webuser/index address and you'll see the results you want.

The catalog results are as follows

Thank you for reading!

Back to Catalog

Love mvc~ in the multi-level folder of views

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.