asp.net mvc - MVC 4.0 Routing Issue -
i building out custom section in site, , has multiple routes, have controller set follows:
- controller - exporttoexcel action
- eomspreasheet parameters
- eomdate, siteid
in routeconfig have following set up.
routes.maproute(name: "eomcoversheet", url: "{controller}/{action}/{eomdate}/{siteid}", defaults: new { controller = "excelexport", action = "eomspreasheet", eomdate = urlparameter.optional, siteid= urlparameter.optional });
when make call controller, , in code stepping through it, values eomdate , siteid null. when @ routedata can see action , controller, parameters route have in routeconfig file.
i have done numerous times in past, first time have seen behavior , not sure how resolve it. ye can provide appreciated.
two things note .net routing are:
- the routes executed in order first registered last, , first match wins.
- route tokens (such
{controller}
) match url segment unless put constraint on them.
this means route posted match any url 2, 3, or 4 segments in it. not useful if have default route matches any url 0, 1, 2, or 3 segments because have large amount of overlap , depending on order declared, 1 of them override other.
one practical solution use literal value route segment, , place route before more generic routes.
routes.maproute(name: "eomcoversheet", url: "excelexport/{action}/{eomdate}/{siteid}", defaults: new { controller = "excelexport", action = "eomspreasheet", eomdate = urlparameter.optional, siteid= urlparameter.optional });
what make route match url if begins excelexport
segment , has 0, 1, 2, or 3 more segments in them.
/excelexport/ /excelexport/eomspreadsheet/ /excelexport/eomspreadsheet/2015-01-01/ /excelexport/eomspreadsheet/2015-01-01/123
if want match url if contains 4 segments, can make segments required instead of optional.
routes.maproute(name: "eomcoversheet", url: "excelexport/{action}/{eomdate}/{siteid}", defaults: new { controller = "excelexport" });
now route match 4 segment url begins excelexport
.
/excelexport/eomspreadsheet/2015-01-01/123
two other possible solutions problem are:
- use areas.
- use route constraints.
Comments
Post a Comment