c# - Is it possible to use attribute routing and convention based routing in the same controller? -


i have controller called hotelscontroller insert , edit hotels.

it has following setup (method implementation removed simplicity):

[routeprefix("{member_id:int}/hotels")] public class hotelscontroller : applicationcontroller {     [route("delete/{id:int}", name = namedroutes.hoteldelete)]     public actionresult delete(int id)     {     }      [route("new", name = namedroutes.hotelnew)]     public actionresult new()     {     }      [httppost]     [validateinput(false)]     public actionresult new(hoteldataentry hotel)     {     }      [route("edit/{id:int}", name = namedroutes.hoteledit)]     public actionresult edit(int id)     {     }      [httppost]     [validateinput(false)]     public actionresult edit(hoteldataentry hotel)     {     } } 

as can see following routes using attribute routing:

  • delete
  • new (without parameters)
  • edit (without parameters)

the following routes use no attribute routing:

  • new (with parameters)
  • edit (with parameters)

the routing setup in global.asax.cs follows:

    public static void registerroutes(routecollection routes)     {         routes.ignoreroute("{resource}.axd/{*pathinfo}");         routes.ignoreroute("{resource}.aspx/{*pathinfo}");         routes.ignoreroute("{resource}.ashx/{*pathinfo}");         routes.ignoreroute("{resource}.asmx/{*pathinfo}");          routes.mapmvcattributeroutes();          routes.maproute(             routen.standard.tostring(),             "{member_id}/{controller}/{action}/{id}",             new { action = "browse", id = urlparameter.optional },             new { id = allowedidsregexoptional }         );     } 

problem: attribute routing works. can call edit action http://localhost:54868/301011/hotels/edit form on page should post same uri , call action uses no attribute routing. instead action using attribute based routing called again. why?

the form supplied method="post". have idea why convention based route not used? thank help.

edit: tried add [httpget] in front of attribute-routed new , edit actions. result on posting form asp.net shows error route invalid. reasons, convention based routing not working on controller.

it seems cannot use both (attribute-based , conventino-based) routing techniques in same controller.

so did resolve issue add attribute-based routes 2 "unreachable" action methods. route of these methods same route of actions same name, name of route different (since route-names must unique).

[routeprefix("{member_id:int}/hotels")] public class hotelscontroller : applicationcontroller {     [route("delete/{id:int}", name = namedroutes.hoteldelete)]     public actionresult delete(int id)     {     }      [route("new", name = namedroutes.hotelnew)]     public actionresult new()     {     }      [httppost]     [validateinput(false)]     [route("new", name = namedroutes.hotelnewpost)]     public actionresult new(hoteldataentry hotel)     {     }      [route("edit/{id:int}", name = namedroutes.hoteledit)]     public actionresult edit(int id)     {     }      [httppost]     [validateinput(false)]     [route("edit/{id:int}", name = namedroutes.hoteleditpost)]     public actionresult edit(hoteldataentry hotel)     {     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -