asp.net mvc - MVC3 areas and routing structure -
i making areas user, admin, shop, forum... etc
in each area, has own controllers, models, views folders. @ root models, root controllers , root views have shared components in them.
structure a:
root/ -> models/, controllers/, views/ root/user/ -> models/, controllers/, views/ root/admin/ -> models/, controllers/, views/ root/shop/ -> models/, controllers/, views/ root/forum/ -> models/, controllers/, views/
i maybe wrong, don't dry me repeat m v , c folders in each of business logic groups. thinking better structure using m v , c main folders , layout business logic groups in them:
structure b:
root/views/ -> user/, admin/, shop/, forum/ ...etc root/models/ -> user/, admin/, shop/, forum/ ...etc root/controllers/ -> user/, admin/, shop/, forum/ ...etc
but if structure folders way, lost area (or users point of view, sub folder path) ability divde logical functionalities of website.
e.g.
with structure a, can do:
www.mywebsite.com/users(area)/account(controller)/logon(action) www.mywebsite.com/admin(area)/account(controller)/logon(action)
notice can have same controller , action names different areas.
with structure b, best can is:
www.mywebsite.com/adminaccount(controller)/logon(action) www.mywebsite.com/useraccount(controller)/logon(action)
it can not achieve single-word sub-folder result without area. if not that, controller names here can longer , more messy soon. not mention have large group of actions stacking in same controller.cs file.
so, anyway, point is, if find structure b making more sense me, how go configuring routing achieve that?
so want structure b url's corresponding a?
you'll giving convention on configuration.
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "useraccount", // route name "users/account/{action}/{id}", // url parameters new { controller = "useraccount", action = "index", id = urlparameter.optional } // parameter defaults ); routes.maproute( "default", // route name "{controller}/{action}/{id}", // url parameters new { controller = "home", action = "index", id = urlparameter.optional } // parameter defaults ); }
another option write custom route handler , introduce own convention. following (new convention concatenate users , account usersaccountcontroller in www.mywebsite.com/users/account/logon). did not test see how handles areas, if have issues let me know , can take look.
public class customconventionroutehandler : mvcroutehandler { protected override ihttphandler gethttphandler(system.web.routing.requestcontext requestcontext) { string controller = requestcontext.routedata.values["controller"].tostring(); object controllermodifier; if (requestcontext.routedata.values.trygetvalue("controllermodifier", out controllermodifier)) { requestcontext.routedata.values["controller"] = string.concat(controllermodifier, controller); } return base.gethttphandler(requestcontext); } } public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.add( new route( "{controllermodifier}/{controller}/{action}", new routevaluedictionary(new { controllermodifier = urlparameter.optional, controller = "home", action = "index" }), //defaults new customconventionroutehandler())); }
Comments
Post a Comment