c# - MVC routing with "optional" routes -


i'm bit baffled how mvc figuring out routing details. let me see if can explain right.

so ... given have default route ...

routes.maproute(     "default",     "{controller}/{action}/{id}",     new { controller = "cms", action = "getpage", id = urlparameter.optional } ); 

and app content management system want create nice urls site structure i'll map wildcard url lets me determine if need render 404 based on what's in database ...

routes.maproute(     "cms",     "{*path}",      new { controller = "cms", action = "getpage", path = string.empty }  ); 

herein lies problem.

mvc match default route because technically no params required assuming "getpage" on "cms" controller requires no params, not want.

what i'm trying "given 2 or 3 url parts, controller , action match optional id parameter other urls including ones can't match route fall down in cms route".

the "easy" way found change first route ...

routes.maproute(     "default",     "get/{controller}/{action}/{id}",     new { controller = "cms", action = "getpage", id = urlparameter.optional } ); 

then url starts "get/" match route , other routes automatically fall down in second route, doesn't sit right somewhere in head , can't figure out quite why yet (i think it's because doesn't solve problem moves it).

my problem don't want route says "given no values match route anyway" changed ...

routes.maproute(     "default",     "{controller}/{action}/{id}" ); 

now odd reason literally every request hitting catch (not quite want close).

so ideas guys?

edit:

i came touch closer ...

    routes.maproute(         "default",          "{controller}/{action}/{id}",          new { id = urlparameter.optional}     ); 

... matches urls 2 parts "foo/bar" rather dropping through should other route because there no "foo" controller.

ok have solution, works me because 99% of requests need map cms route in case may not if have lot of controllers need map to.

i hoping find ideal solution merely ideal in scenario ...

so assuming have (like me) cms controller , accounts controller can this:

    routes.maproute("account", "account/{action}", new { controller = "account" });      routes.maproute(         "default",         "{*path}",          new { controller = "cms", action = "getpage", path = string.empty }      ); 

that way urls starting "account" caught first rule, , else falls through default route , gets handled cms controller.

i plan add more routes add more controllers. it's not ideal solution because mean end lot of route mappings in long term enough solution meet needs.

hope helps else out there.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -