Reputation: 13818
How do I form routes to use the same method on different controllers in ASP.Net Web API?
Here's What I've tried :
var config = new HttpSelfHostConfiguration(strUrl);
config.Routes.MapHttpRoute(
"Scripts", "{controller}/{Name}",
new { controller = "Scripts",strScriptId=""});
config.Routes.MapHttpRoute(
"Images", "{controller}/{strParam}",
new { controller = "Images", strImageId= "" });
Thanks.
Edit: Same method in the sense..for example, a method accepting just one parameter on with a different name on two controllers.
Example :
I need to access it like
1. http://localhost/GetScripts/ScriptId123
2. http://localhost/GetImages/ImageId223
I'm unable to figure out the routes for this. I have tried the above routes (edited them to make it clearer.)
I have many more such controllers with different getsomething methods accepting just one parameter.
Upvotes: 0
Views: 1408
Reputation: 542
AS We all know REST is resource based and this identify the resource with the URL, so that not more than one method with same parameter will be allowed in the REST service but there is work around in MVC 5 Web Api method level routing.
Here is the example you can do that:
[HttpGet]
[Route("api/search/FindByName/{name}")]
FindByName(string name)
{
}
[HttpGet]
[Route("api/search/FindById/{name}")]
FindById(int searchId)
Note:"search" is the controller name.
Please let know if need more clarification.
Upvotes: 0
Reputation: 2156
The default route:
routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Should work for you. Make sure your action methods matches the signature (use id instead of strImageId and strScriptId)
EDIT:
@Josh the parameters in the route SHOULD match the parameters in your actions. As I said before, there is NO NEED to create alternate routes but if you are required to preserve those names on the ids, you can create a route to accommodate them:
routes.MapHttpRoute(
name: "scriptsRoute",
routeTemplate: "{controller}/{strScriptId}",
defaults: new { controller="Scripts" }
);
routes.MapHttpRoute(
name: "imagesRoute",
routeTemplate: "{controller}/{strImageId}",
defaults: new { controller="Images" }
);
Please NOTE in the routes above the ids are not optional, you can add the constrain if needed. Also what @liel is recommending in terms of naming your controllers/actions is good advise.
Upvotes: 0
Reputation: 57949
You could just have one single route like below:
config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new {id = RouteParameter.Optional});
and you could have your actions like below:
GetImages([FromUri(Name="id")] string strImageId);
GetScripts([FromUri(Name="id")] string strScriptId);
Upvotes: 2
Reputation: 2437
Please refer to this tutorial for routing options.
For Id
parameter it's the easiest. Routing should be:
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
and the controller method (it doesn't matter on which controller!):
public MyData Get(int id)
{
...
}
If you really want string for Id, you can just use it as follows:
public MyData Get(string id)
{
...
}
Do you really need a different name for the parameter?
If you do, you can simply try the following routing (I am not sure, though):
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}"
);
Also please note that the routing routes you wrote are conceptually wrong for WebApis:
Instead of
Consider the following (For Get requests):
Upvotes: 0