Multiple Similar Route not working in ASP.NET MVC 4 -


i have 2 routes, of them not working on same time. 1 on top working fine bottom 1 not works

 config.routes.maphttproute(                 name: "getkeywordsearch",                 routetemplate: "api/{controller}/{action}/{keyword}/{selection}"                 //defaults: new { selection = routeparameter.optional }             );                         config.routes.maphttproute(                name: "getchapter",                routetemplate: "api/{controller}/{action}/{bookname}/{chapternum}/"                 //defaults: new {}             ); 

any suggestion?

those 2 routes have same pattern. there's absolutely no difference between them , routing engine has no way of disambiguating them. , since routes evaluated in order in defined, first 1 picked.

in order make work need constrain parameters using regular expressions or custom constraints. example:

config.routes.maphttproute(     name: "getkeywordsearch",     routetemplate: "api/{controller}/{action}/{keyword}/{selection}",     constraints: new { keyword = @"[0-9]" },     defaults: new { selection = routeparameter.optional } );  config.routes.maphttproute(     name: "getchapter",     routetemplate: "api/{controller}/{action}/{bookname}/{chapternum}"     constraints: new { bookname = @"[a-z]" },     defaults: new { chapternum = routeparameter.optional } ); 

in example provided, keyword can contain numbers whereas bookname letters. way routing engine able disambiguate between following urls , dispatch them proper controller action:

api/somecontroller/someaction/123 api/somecontroller/someaction/foo 

obviously have adapt regular expressions match specific needs remember there must pattern make difference.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -