i'm developing simple crud application using asp mvc 1 store data across multiple tables multiple student programs. in doing so, i'm trying figure out how structure url accommodate each program, tables, , actions.
for example, trying achieve is:
site.com/studprog1/participant/create url inserting entry participant table student program 1
site.com/studprog2/course/ url index page course table student program 2
in attempt create custom routes accommodate this, global.asax.cs file stands follows:
public class mvcapplication : system.web.httpapplication { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "studprog1", "studprog1/{pagename}/{action}", new { controller = "studprog1", pagename="index", action = "index" } ); routes.maproute( "studprog2", "studprog2/{pagename}/{action}", new { controller = "studprog2", pagename="index", action = "index" } ); routes.maproute( "default", // route name "{controller}/{action}/{id}", // url parameters new { controller = "home", action = "index", id = urlparameter.optional } // parameter defaults ); } }
where pagename supposed table name.
not surprisingly, above structure not return output aiming (i.e. points index page studprog).
additionally, file directory structured such:
views studprog1 index participant index create studprog2 index courses index create
etc.
my question is, how can improve routes correctly achieve url structure desire. additionally, besides microsoft asp.net developer network site, there tutorials on custom routes?
you should not creating controller each student program (studprog1,studprog2 etc). can accept parameter.
you need 2 controlellers, 1 particiant , 1 course.
public class participantcontroller : controller { public actionresult index() { return content("reques partiicpant index page if needed"); } public actionresult create(string studentprogram) { return content("partiicpant create "+studentprogram); } } public class coursecontroller : controller { public actionresult index() { return content("course:"); } }
and in routeconfig's registerroutes method, specify 2 specific routes before generic default route.
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute("part","{studentprogram}/participant/{action}", new { controller = "participant", action = "index" } ); routes.maproute("course", "{studentprogram}/course/{action}", new { controller = "course", action = "index" } ); routes.maproute("default", "{controller}/{action}/{id}", new { controller = "home", action = "index", id = urlparameter.optional }); }
Comments
Post a Comment