网络核心2中的内容类型的Web api属性路由?

时间:2018-05-09 03:37:06

标签: asp.net-core-mvc asp.net-core-2.0

我希望能够在同一个网址上使用已发布的JSON或表单数据。

根据情况,我得到:

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/建议使用不同的端点,但在这种情况下我不能这样做。

https://massivescale.com/web-api-routing-by-content-type/建议为asp.net做一个方法,例如:

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

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

但在.net核心中,我们没有System.Web.Http.Routing。也许它可以被移植到使用Microsoft.AspNetCore.Mvc.Routing ...但是有什么可以替代IHttpRouteConstraint

我的问题:.net core mvc已经内置了这样的东西吗?

例如,在Java的JAX-RS中,有@Consumes(“application / json”)

1 个答案:

答案 0 :(得分:9)

我通过Consumes属性完成了此操作:

http://example.com/payment/callback-接受x-www-form-urlencoded。

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

    }

http://example.com/payment/callback-相同的URL,但是接受application / json。

redirect()->to()