Is there a default verb applied to a Web API ApiController method?

File this under learning something new every day! Typically method name matching is thought of this way. Looking at the WebAPI source, however, there is a branch of logic for fallback. If the method name doesn’t map (through attribute, or convention) to a supported HTTP verb, then the default is POST. By default action selection … Read more

Multiple Controller Types with same Route prefix ASP.NET Web Api

Web API (1.x-2.x) does not support multiple attribute routes with the same path on different controllers. The result is a 404, because all the route matches more than one controller and at that point Web API will consider the result ambiguous. Note that MVC Core does support this scenario note: MVC Core serves as both … Read more

Routing with multiple Get methods in ASP.NET Web API

From here Routing in Asp.net Mvc 4 and Web Api Darin Dimitrov has posted a very good answer which is working for me. It says… You could have a couple of routes: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: “ApiById”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional }, … Read more

Query string not working while using attribute routing

I was facing the same issue of ‘How to include search parameters as a query string?’, while I was trying to build a web api for my current project. After googling, the following is working fine for me: Api controller action: [HttpGet, Route(“search/{categoryid=categoryid}/{ordercode=ordercode}”)] public Task<IHttpActionResult> GetProducts(string categoryId, string orderCode) { } The url I tried … Read more

Multiple actions were found that match the request in Web Api

Your route map is probably something like this in WebApiConfig.cs: routes.MapHttpRoute( name: “API Default”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional }); But in order to have multiple actions with the same http method you need to provide webapi with more information via the route like so: routes.MapHttpRoute( name: “API Default”, routeTemplate: “api/{controller}/{action}/{id}”, defaults: … Read more

FromBody string parameter is giving null

By declaring the jsonString parameter with [FromBody] you tell ASP.NET Core to use the input formatter to bind the provided JSON (or XML) to a model. So your test should work, if you provide a simple model class public class MyModel { public string Key {get; set;} } [Route(“Edit/Test”)] [HttpPost] public void Test(int id, [FromBody] … Read more

Multiple HttpPost method in Web API controller

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 … Read more