gpt4 book ai didi

asp.net-core-mvc - 在点网核心2中按内容类型进行Web api属性路由?

转载 作者:行者123 更新时间:2023-12-04 16:42:09 27 4
gpt4 key购买 nike

我希望能够使用同一URL使用发布的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核心mvc中已经内置了这样的东西吗?

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

最佳答案

我是通过Consumes属性完成此操作的:
http://example.com/payment/callback-接受x-www-form-urlencoded。

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

}
http://example.com/payment/callback-相同的URL,但接受application/json。
[HttpPost]
[Route("callback")]
[Consumes("application/json")]
public ActionResult Post([FromBody] JObject value)
{

}

关于asp.net-core-mvc - 在点网核心2中按内容类型进行Web api属性路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com