在点网核心2中按内容类型进行Web api属性路由?

编程入门 行业动态 更新时间:2024-10-25 06:23:42
本文介绍了在点网核心2中按内容类型进行Web api属性路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够使用同一URL使用发布的JSON或表单数据.

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

按现状,我会得到:

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:

andrewlock/model-binding -json-posts-in-asp-net-core/建议使用不同的端点,但是在这种情况下我不能这样做.

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

massivescale/web-api-routing-by -content-type/提出了一种用于asp的方法,例如:

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

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

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

但是在核心中,我们没有System.Web.Http.Routing.也许可以将其移植为使用Microsoft.AspNetCore.Mvc.Routing ...,但是是否可以替换IHttpRouteConstraint

but in 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

我的问题:核心mvc中已经内置了这样的东西吗?

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

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

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

推荐答案

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

I accomplished this by the Consumes attribute:

example/payment/callback -接受x-www-form-urlencoded

example/payment/callback - Accepts x-www-form-urlencoded.

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

example/payment/callback -相同的网址,但接受application/json.

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

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

更多推荐

在点网核心2中按内容类型进行Web api属性路由?

本文发布于:2023-11-16 11:21:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   属性   核心   类型   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!