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