Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web api attribute routing, by content type, in dot net core 2?

I'd like to be able to consume either posted JSON or form data at the same URL.

As things stand, I get:

fail: Microsoft.AspNetCore.Mvc.Internal.ActionSelector[1]
      Request matched multiple actions resulting in ambiguity. Matching actions: 
:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLDLB0LJCPJ4", Request id "0HLDLB0LJCPJ4:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

https://andrewlock.net/model-binding-json-posts-in-asp-net-core/ suggests using different endpoints, but I can't do that in this case.

https://massivescale.com/web-api-routing-by-content-type/ suggests a way to do it for asp.net, for example:

[ContentTypeRoute("api/test/bytype", "application/json")]

or

[ContentTypeRoute("api/test/bytype", "application/x-www-form-urlencoded")]

but in .net core, we don't have System.Web.Http.Routing. Maybe it can be ported to use Microsoft.AspNetCore.Mvc.Routing... but is there something to replace IHttpRouteConstraint

My question: is something like this already built into .net core mvc?

For example, in Java's JAX-RS, there is @Consumes("application/json")

like image 967
JasonPlutext Avatar asked May 09 '18 03:05

JasonPlutext


People also ask

How do I enable attribute routing in Web API 2?

To enable attribute routing, call MapHttpAttributeRoutes during configuration. This extension method is defined in the System. Web. Http.

Which of the following type of routing is supported in Web API 2?

Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

How do I specify a route in Web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config. MapHttpAttributeRoutes() method.

What is attribute routing in .NET core?

With attribute-based routing, we can use C# attributes on our controller classes and on the methods internally in these classes. These attributes have metadata that tell ASP.NET Core when to call a specific controller. It is an alternative to convention-based routing.


1 Answers

I accomplished this by the Consumes attribute:

http://example.com/payment/callback - Accepts x-www-form-urlencoded.

[HttpPost]
[Route("callback")]
[Consumes("application/x-www-form-urlencoded")]
public ActionResult Post([FromForm] string value)
{

}

http://example.com/payment/callback - Same url but accepts application/json.

[HttpPost]
[Route("callback")]
[Consumes("application/json")]
public ActionResult Post([FromBody] JObject value)
{

}
like image 114
Vyas Bharghava Avatar answered Sep 21 '22 16:09

Vyas Bharghava