I have two methods in my Web API controller as follows:
public samplecontroller: webapicontroller
{
[HttpPost]
public void PostMethod()
[HttpGet]
public void GetValues(int a,int b)
}
I have the following in global.asax:
routes.MapHttpRoute
("Default API Route", "api/{controller}/{id1}/{id2}/{id3}/{id4}/{id5}",
new { id1 = UrlParameter.Optional, id2 = UrlParameter.Optional, id3 = UrlParameter.Optional, id4 = UrlParameter.Optional, id5 = UrlParameter.Optional });
If I want to call the second method i.e., GetValues(int a,int b), can I write one more HttpRoute in Global.asax as follows?
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{Sample}/{GetValues}/{a}/{b}",
defaults: new { a = UrlParameter.Optional, b=UrlParameter.Optional }
);
So can I create more than one maproute in global.asax?
And, to provide the optional parameter, should I give the same as parameters like a and b only?
You can have multiple routes in global.asax; each incoming request will go to the first route that matches. That means that the more specific ones should go first. A URL matches a route if:
controllerandactionroute values are defined.That said, your proposed route api/{Sample}/{GetValues}/{a}/{b} doesn’t make sense. It creates two new (meaningless) route values
SampleandGetValues, and doesn’t provide a definitioncontrolleroraction. I think what you meant to write is this:This will match the URI
/api/Sample/GetValues/1/2to your action.