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

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

Optional Parameters in Web Api Attribute Routing

For an incoming request like /v1/location/1234, as you can imagine it would be difficult for Web API to automatically figure out if the value of the segment corresponding to ‘1234’ is related to appid and not to deviceid. I think you should change your route template to be like [Route(“v1/location/{deviceOrAppid?}”, Name = “AddNewLocation”)] and then … 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

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