Click here to Skip to main content
14,426,749 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Guys,
I have one issue with WebApi routing. Please find the code snippet below-

config.Routes.MapHttpRoute(
               name: "reportsApi",
               routeTemplate: "api/{controller}/list/",
               defaults: new { reports = RouteParameter.Optional  }
           );


Earlier it was working just fine and there is no change in the code of report module and it stopped working after the deployment of another version. When i remove '/list' from 'https://whatever.com/api/report/list it works fine as expected.

Any idea? Any help will be highly appreciated.

Thanks
Amod
Posted
Updated 14-Jun-15 20:26pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Your routing isn't quite correct, largely because you're referencing a parameter that isn't there. If you include a default option, you should also have a placeholder in the template to match it:

For instance (based on the URL in your question):
config.Routes.MapHttpRoute(
               name: "reportsApi",
               routeTemplate: "api/{controller}/{reports}/list/",
               defaults: new { reports = RouteParameter.Optional  }
           );


Looking at it, I'm not sure what the 'list' is doing for you in that particular route, but based on your URL I can only assume it's either ReportController.List(), or an acceptable id parameter for ReportController.Get(). My supposition is that it's using the default route when you change the address.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi
I agree with Nathan, in your routing you have mentioned reports as defaults but then why is not seen in the route template..
config.Routes.MapHttpRoute(
               name: "reportsApi",
               routeTemplate: "api/{controller}/list/",
               defaults: new { reports = RouteParameter.Optional  }
           );


Try adding reports to route template as below..

config.Routes.MapHttpRoute(
               name: "reportsApi",
               routeTemplate: "api/{controller}/{reports}/list/",
               defaults: new { reports = RouteParameter.Optional  }
           );


Hope this helps..
   
v4

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100