webapi multiple methods in MapHttpRoute - c#

Using route annotations in asp.net core I can do the following easily enough
[HttpGet("ByID/{id}")]
public int GetByID(int id)
{
return 5;
}
[HttpGet("ByName/{id}")]
public string GetByName(string name)
{
return "neil";
}
However, with the same methods in the controller how do you do it using the non-annotation way in the WebApiConfig file (i.e. .net framework), i.e. what do I add to the below to make it work for the above example.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
As a bonus, can somebody tell me how I would have the same URL map to two GET items differentiated only by type, e.g.
[HttpGet("{id}")]
[HttpGet("{name}")]
where one is an integer and one is a string - in other words the same as the above but without having to specify two separate url segments (ByID, ByName)?

You can use route constraints to differentiate the actions for the same path.
[RoutePrefix("api/somepath")]
public class MyController : ApiController {
//Matches GET api/somepath/5
[HttpGet]
[Route("{id:int}")]
public int GetByID(int id) {
return id;
}
//Matches GET api/somepath/neil
[HttpGet]
[Route("{name}")]
public string GetByName(string name) {
return name;
}
}
Note the default is string which is why there is none on the second action.

Related

Can I have multiple POST methods in WebAPI/MVC on same route? [duplicate]

I am starting to use MVC4 Web API project, I have controller with multiple HttpPost methods. The Controller looks like the following:
Controller
public class VTRoutingController : ApiController
{
[HttpPost]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
Here MyRequestTemplate represents the template class responsible for handling the Json coming through the request.
Error:
When I make a request using Fiddler for http://localhost:52370/api/VTRouting/TSPRoute or http://localhost:52370/api/VTRouting/Route I get an error:
Multiple actions were found that match the request
If I remove one of the above method it works fine.
Global.asax
I have tried modifying the default routing table in global.asax, but I am still getting the error, I think I have problem in defining routes in global.asax. Here is what I am doing in global.asax.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapHttpRoute(
name: "MyTSPRoute",
routeTemplate: "api/VTRouting/TSPRoute",
defaults: new { }
);
routes.MapHttpRoute(
name: "MyRoute",
routeTemplate: "api/VTRouting/Route",
defaults: new { action="Route" }
);
}
I am making the request in Fiddler using POST, passing json in RequestBody for MyRequestTemplate.
You can have multiple actions in a single controller.
For that you have to do the following two things.
First decorate actions with ActionName attribute like
[ActionName("route")]
public class VTRoutingController : ApiController
{
[ActionName("route")]
public MyResult PostRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[ActionName("tspRoute")]
public MyResult PostTSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
Second define the following routes in WebApiConfig file.
// Controller Only
// To handle routes like `/api/VTRouting`
config.Routes.MapHttpRoute(
name: "ControllerOnly",
routeTemplate: "api/{controller}"
);
// Controller with ID
// To handle routes like `/api/VTRouting/1`
config.Routes.MapHttpRoute(
name: "ControllerAndId",
routeTemplate: "api/{controller}/{id}",
defaults: null,
constraints: new { id = #"^\d+$" } // Only integers
);
// Controllers with Actions
// To handle routes like `/api/VTRouting/route`
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}"
);
Another solution to your problem would be to use Route which lets you specify the route on the method by annotation:
[RoutePrefix("api/VTRouting")]
public class VTRoutingController : ApiController
{
[HttpPost]
[Route("Route")]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost]
[Route("TSPRoute")]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
use:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
it's not a RESTful approach anymore, but you can now call your actions by name (rather than let the Web API automatically determine one for you based on the verb) like this:
[POST] /api/VTRouting/TSPRoute
[POST] /api/VTRouting/Route
Contrary to popular belief, there is nothing wrong with this approach, and it's not abusing Web API. You can still leverage on all the awesome features of Web API (delegating handlers, content negotiation, mediatypeformatters and so on) - you just ditch the RESTful approach.
A web api endpoint (controller) is a single resource that accepts get/post/put/delete verbs. It is not a normal MVC controller.
Necessarily, at /api/VTRouting there can only be one HttpPost method that accepts the parameters you are sending. The function name does not matter, as long as you are decorating with the [http] stuff. I've never tried, though.
Edit: This does not work. In resolving, it seems to go by the number of parameters, not trying to model-bind to the type.
You can overload the functions to accept different parameters. I am pretty sure you would be OK if you declared it the way you do, but used different (incompatible) parameters to the methods. If the params are the same, you are out of luck as model binding won't know which one you meant.
[HttpPost]
public MyResult Route(MyRequestTemplate routingRequestTemplate) {...}
[HttpPost]
public MyResult TSPRoute(MyOtherTemplate routingRequestTemplate) {...}
This part works
The default template they give when you create a new one makes this pretty explicit, and I would say you should stick with this convention:
public class ValuesController : ApiController
{
// GET is overloaded here. one method takes a param, the other not.
// GET api/values
public IEnumerable<string> Get() { .. return new string[] ... }
// GET api/values/5
public string Get(int id) { return "hi there"; }
// POST api/values (OVERLOADED)
public void Post(string value) { ... }
public void Post(string value, string anotherValue) { ... }
// PUT api/values/5
public void Put(int id, string value) {}
// DELETE api/values/5
public void Delete(int id) {}
}
If you want to make one class that does many things, for ajax use, there is no big reason to not use a standard controller/action pattern. The only real difference is your method signatures aren't as pretty, and you have to wrap things in Json( returnValue) before you return them.
Edit:
Overloading works just fine when using the standard template (edited to include) when using simple types. I've gone and tested the other way too, with 2 custom objects with different signatures. Never could get it to work.
Binding with complex objects doesn't look "deep", so thats a no-go
You could get around this by passing an extra param, on the query string
A better writeup than I can give on available options
This worked for me in this case, see where it gets you. Exception for testing only.
public class NerdyController : ApiController
{
public void Post(string type, Obj o) {
throw new Exception("Type=" + type + ", o.Name=" + o.Name );
}
}
public class Obj {
public string Name { get; set; }
public string Age { get; set; }
}
And called like this form the console:
$.post("/api/Nerdy?type=white", { 'Name':'Slim', 'Age':'21' } )
It is Possible to add Multiple Get and Post methods in the same Web API Controller. Here default Route is Causing the Issue. Web API checks for Matching Route from Top to Bottom and Hence Your Default Route Matches for all Requests. As per default route only one Get and Post Method is possible in one controller. Either place the following code on top or Comment Out/Delete Default Route
config.Routes.MapHttpRoute("API Default",
"api/{controller}/{action}/{id}",
new { id = RouteParameter.Optional });
When creating another Http Method add [HttpPost("Description")]
[HttpPost("Method1")]
public DataType Method1(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost("Method2")]
public DataType Method2(MyRequestTemplate routingRequestTemplate){}
Put Route Prefix [RoutePrefix("api/Profiles")] at the controller level and put a route at action method [Route("LikeProfile")]. Don't need to change anything in global.asax file
namespace KhandalVipra.Controllers
{
[RoutePrefix("api/Profiles")]
public class ProfilesController : ApiController
{
// POST: api/Profiles/LikeProfile
[Authorize]
[HttpPost]
[Route("LikeProfile")]
[ResponseType(typeof(List<Like>))]
public async Task<IHttpActionResult> LikeProfile()
{
}
}
}
You can use this approach :
public class VTRoutingController : ApiController
{
[HttpPost("Route")]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost("TSPRoute")]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
I think the question has already been answered. I was also looking for something a webApi controller that has same signatured mehtods but different names. I was trying to implement the Calculator as WebApi. Calculator has 4 methods with the same signature but different names.
public class CalculatorController : ApiController
{
[HttpGet]
[ActionName("Add")]
public string Add(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Add = {0}", num1 + num2);
}
[HttpGet]
[ActionName("Sub")]
public string Sub(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Subtract result = {0}", num1 - num2);
}
[HttpGet]
[ActionName("Mul")]
public string Mul(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Multiplication result = {0}", num1 * num2);
}
[HttpGet]
[ActionName("Div")]
public string Div(int num1 = 1, int num2 = 1, int timeDelay = 1)
{
Thread.Sleep(1000 * timeDelay);
return string.Format("Division result = {0}", num1 / num2);
}
}
and in the WebApiConfig file you already have
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
Just set the authentication / authorisation on IIS and you are done!
Hope this helps!
Best and simplest explanation I have seen on this topic -
http://www.binaryintellect.net/articles/9db02aa1-c193-421e-94d0-926e440ed297.aspx
Edited -
I got it working with only Route, and did not need RoutePrefix.
For example, in the controller
[HttpPost]
[Route("[action]")]
public IActionResult PostCustomer
([FromBody]CustomerOrder obj)
{
}
and
[HttpPost]
[Route("[action]")]
public IActionResult PostCustomerAndOrder
([FromBody]CustomerOrder obj)
{
}
Then, the function name goes in jquery as either -
options.url = "/api/customer/PostCustomer";
or
options.url = "/api/customer/PostCustomerAndOrder";
I am using .Net6. please find the following code. I have achieve like the following.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ProjectName.Controllers
{
[Route("[controller]")]
[ApiController]
public class WizardAPIController : ControllerBase
{
[HttpGet("Methord1")]
public async Task<IActionResult> Methord1()
{
return Ok("all good");
}
[HttpGet("Methord2")]
public async Task<IActionResult> Methord2()
{
return Ok("all good");
}
}
}
public class Journal : ApiController
{
public MyResult Get(journal id)
{
return null;
}
}
public class Journal : ApiController
{
public MyResult Get(journal id, publication id)
{
return null;
}
}
I am not sure whether overloading get/post method violates the concept of restfull api,but it workds. If anyone could've enlighten on this matter. What if I have a uri as
uri:/api/journal/journalid
uri:/api/journal/journalid/publicationid
so as you might seen my journal sort of aggregateroot, though i can define another controller for publication solely and pass id number of publication in my url however this gives much more sense. since my publication would not exist without journal itself.

Web API Routing Not Recognising Parameters

I have the following WebAPI controller:
namespace MyApp.WebApi.Controllers
{
[RoutePrefix("api/listing")]
public class ListingController : ApiController
{
[Route("{firstparam:int?}/{nextparam:int?}")]
public IEnumerable<ListItem> Get(int firstparam = 100, int nextparam = 12)
{
// firstparam is always 100, and nextparam is always 12
However, I've tried specifying the URL:
http://localhost:56004/#/listing?firstparam=2
If I specify the URL like this:
http://localhost:56004/#/listing/2
Then it breaks the routing.
Clearly I'm missing something regarding routing; please could someone point me in the right direction?
You are using multiple optional parameter which don't work well for routeTemplates. Normally the last parameter tends to be the optional parameter.
Documentation: Attribute Routing in ASP.NET Web API 2: Optional URI Parameters and Default Values
FIrst make sure the attribute routing is enabled
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
To get what you are after
[RoutePrefix("api/listing")]
public class ListingController : ApiController {
//GET api/listing
//GET api/listing?firstparam=x
//GET api/listing?nextparam=y
//GET api/listing?firstparam=x&nextparam=y
[HttpGet]
[Route("")]
public IEnumerable<ListItem> Get(int firstparam = 100, int nextparam = 12) { ... }
}
The problem with having multiple inline parameters that are optional is that the router wont know which to use which is why they tend to be at the end of the url.
However to get them inline like how you mentioned in your example you are going to need multiple routes.
[RoutePrefix("api/listing")]
public class ListingController : ApiController {
//GET api/listing
[HttpGet]
[Route("")]
public IEnumerable<ListItem> Get() { return Get(100, 12); }
//GET api/listing/2
//GET api/listing/2/5
[HttpGet]
[Route("{firstparam:int}/{nextparam:int?}")]
public IEnumerable<ListItem> Get(int firstparam, int nextparam = 12) { ... }
}
You could try using [FromUri] attribute within the params of the Get() method to extract any query params being passed into the "/listing" uri along with a class that consists of int properties firstparam and secondparam.
namespace MyApp.WebApi.Controllers
{
[RoutePrefix("api/listing")]
public class ListingController : ApiController
{
[Route("")]
[HttpGet]
public IEnumerable<ListItem> Get([FromUri] ClassRepresentingParams params)
{
Hopefully that helps.

Why do WebApi routing config defaults not include the action param?

I am a bit new to WebApi so maybe someone can explain this to me, the default route added for WebApis is:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
So given an ApiController that looks like this:
public class LookupController : ApiController
{
[HttpGet]
public IHttpActionResult GetCountries()
{
// do stuff
return Ok();
}
[HttpGet]
public IHttpActionResult GetStates()
{
// do stuff
return Ok();
}
}
How would it know which action to call? It wouldn't right?
Shouldn't the default Route be more like:
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Or should you only have one GET/UPDATE/DELETE etc per ApiController? Which really wouldn't cater for some scenarios...
Web API figures out which method to call based on routing data. You don't need to specify the action because Web API will use the verb(GET, POST,DELETE,etc) of the request.
If you would like to have multiple GET actions in a controller, you can specify a route for each action.
[Route("api/lookup/countries"]
[HttpGet]
public IHttpActionResult GetCountries()
{
// do stuff
return Ok();
}
[Route("api/lookup/states"]
[HttpGet]
public IHttpActionResult GetStates()
{
// do stuff
return Ok();
}
More information is available here http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
Strictly speaking your controller should never look like that. Out of the box the idea is that a controller handles requests for a single entity (e.g. Customer) and the operations, which map to HTTP verbs, operate against that entity. So under normal circumstances your controller would look something like the following (created using scaffolding against a simple model class, method bodies omitted for brevity):
public class CustomerController : ApiController
{
public IQueryable<Customer> GetCustomers()
{
}
[ResponseType(typeof(Customer))]
public IHttpActionResult GetCustomer(int id)
{
}
[ResponseType(typeof(void))]
public IHttpActionResult PutCustomer(int id, Customer customer)
{
}
[ResponseType(typeof(Customer))]
public IHttpActionResult PostCustomer(Customer customer)
{
}
[ResponseType(typeof(Customer))]
public IHttpActionResult DeleteCustomer(int id)
{
}
}
If you have more business-oriented operations to perform (e.g. BillCustomer), rather than just the basic CRUD operations, I would suggest creating a separate route for those. We did that in one of our applications and it created a nice logical separation. For example:
config.Routes.MapHttpRoute(
name: "RestApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "RpcApi",
routeTemplate: "rpc/{controller}/{action}"
);

How can I specify a specific URL in an API controller?

Right now, my API controller has 2 methods: one to get ALL events, and one to get ONE event.
namespace HobbsEventsMobile.Controllers
{
public class EventController : ApiController
{
// GET api/event
[HttpGet]
public List<HobbsEventsMobile.Models.Event> Get()
{
return HobbsEventsMobile.Models.Event.GetEventSummary();
}
// GET api/event/5
[HttpGet]
public HobbsEventsMobile.Models.Event Get(int id)
{
return HobbsEventsMobile.Models.Event.GetEventDetails(id);
}
}
}
Newly requested functionality requires me to add a way to call events for the current week. I have a stored proc and a method to call this, but I am not sure how to specify the URL. I would like to add this:
[HttpGet]
public List<HobbsEventsMobile.Models.Event> Get()
{
return HobbsEventsMobile.Models.Event.GetThisWeeksEvents();
}
but make it accessible at m.mydomain.com/api/event/thisweek (or something). How do I do that?
You have two different options depending on what version of ASP.NET Web API you're running. If you're on version one you can simply follow the convention based routing and use:
public class EventController : ApiController
{
[HttpGet]
public List<HobbsEventsMobile.Models.Event> ThisWeek()
{
return HobbsEventsMobile.Models.Event.GetThisWeeksEvents();
}
}
You will also need to modify your route definitions to support an action name (by default the framework picks the method based upon the HTTP verb):
config.Routes.MapHttpRoute(
"DefaultApiWithId",
"api/{controller}/{id}",
new { id = RouteParameter.Optional }, new { id = #"\d+" }
);
config.Routes.MapHttpRoute(
"DefaultApiWithAction",
"api/{controller}/{action}"
);
config.Routes.MapHttpRoute(
"DefaultApiGet",
"api/{controller}",
new { action = "Get" },
new { httpMethod = new HttpMethodConstraint("GET") }
);
If you're using version two, you can still use the convention based routing, but you also have the ability to use attribute routing:
public class EventController : ApiController
{
[HttpGet]
[Route("event/thisweek")]
public List<HobbsEventsMobile.Models.Event> ICanNameThisWhateverIWant()
{
return HobbsEventsMobile.Models.Event.GetThisWeeksEvents();
}
}

How to pass/receive multiple args to a RESTful Web API GET method?

The usual examples of GET RESTful methods that take a parameter (returning a scalar value rather than a dataset) are shown like so:
public string Get(int id)
{
//get and return the value
}
...where the val passed is typically an ID, so you can use it to get a scalar value based on that unique value.
What, though, if you want to pass multiple values, such as a string and an int? Is it simply a matter of defining a method like so:
public string Get(string someString, int someInt)
{
//get and return the value
}
...and calling it like so:
//const string uri = "http://192.112.183.42:80/api/platypusItems/someString/someInt";, zB:
const string uri = "http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42";
var webRequest = (HttpWebRequest) WebRequest.Create(uri);
?
IOW, will the routing mechanism figure out that, since two args are passed, it should call the Get() method with two args ("convention over configuration"), or is there more that has to be done to route things appropriately?
If you use Web API 2, then you can use Attribute Routing to route requests like http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42
public class ItemsController : ApiController
{
[Route("api/{controller}/{id}")]
public string GetItemById(int id)
{
// Find item here ...
return item.ToString();
}
[Route("api/{controller}/{name}/{id}")]
public string GetItemByNameAndId(string name, int id)
{
// Find item here ...
return item.ToString();
}
}
http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42 will be mapped to GetItemByNameAndId while http://192.112.183.42:80/api/platypusItems/42 will be mapped to GetItemById.
Note, that you need to enable attribute routing in configuration like this:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
But generally you should pass arguments as additional parameters. It is especially easy with GET requests. This will work in Web API 1&2:
public class ItemsController : ApiController
{
public string GetItemById(int id)
{
// Find item here ...
return item.ToString();
}
public string GetItemByNameAndId(string name, int id)
{
// Find item here ...
return item.ToString();
}
}
Assuming that you have default mapping configuration, http://192.112.183.42:80/api/platypusItems/42 will be mapped to GetItemById while http://192.112.183.42:80/api/platypusItems/42?name=DuckbilledPlatypisAreGuysToo will be mapped to GetItemByNameAndId because Web API can map 2 parameters instead of 1 for GetItemById.
More information can be found in Mike Wasson articles on Attribute Routing, Routing and Action Selection and Routing in Web API.

Categories

Resources