Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tow Routes in Global.asax. Both are correct but the problem is that only route run which is first e.g if my url us api/catagory/GetAllCatagory then it run fine but if my url is api/catagory/Id then it gives error.


C#
routes.MapHttpRoute(
              name: "ActionApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional });




          routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional });
Posted
Updated 24-Oct-14 0:34am
v2

1 solution

Not an expert on routing, but since your id is optional it may be the following.
Let's say you send api/category/id

First route is read and it matches your api/controller/action (since you have three parts and computer does not distinguish the names for meaning) - that is, first route matches as if it is action request without parameters.

Therefore, your route tries to map id from api/category/id to action in api/{controller}/{action}

Try setting id in first route as mandatory.


If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900