我面临一个奇怪的问题,几乎花了4个小时没有运气.
我有一个简单的Web API,我在表单提交上调用.
API -
// POST: api/Tool
[HttpPost]
public void Post([FromBody] Object value)
{
_toolService.CreateToolDetail(Convert.ToString(value));
}
Run Code Online (Sandbox Code Playgroud)
HTML的
<!DOCTYPE html>
<html>
<body>
<h2>HTML Forms</h2>
<form name="value" action="https://localhost:44352/api/tool" method="post">
First name:<br>
<input type="text" id="PropertyA" name="PropertyA" value="Some value A">
<br>
Last name:<br>
<input type="text" id="PropertyB" name="PropertyB" value="Some value B">
<br><br>
<!--<input type="file" id="Files" name="Files" multiple="multiple"/>-->
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我点击提交按钮时,我得到以下错误 -
{"":["The input was not valid."]}
Run Code Online (Sandbox Code Playgroud)
启动课程中的配置 -
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IConfiguration>(Configuration);
}
public void Configure(IApplicationBuilder …Run Code Online (Sandbox Code Playgroud) 我希望能够在同一个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:
Run Code Online (Sandbox Code Playgroud)
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")]
Run Code Online (Sandbox Code Playgroud)
要么
[ContentTypeRoute("api/test/bytype", "application/x-www-form-urlencoded")]
Run Code Online (Sandbox Code Playgroud)
但在.net核心中,我们没有System.Web.Http.Routing.也许它可以被移植到使用Microsoft.AspNetCore.Mvc.Routing ...但是有什么东西可以取代IHttpRouteConstraint
我的问题:.net core mvc已经内置了这样的东西吗?
例如,在Java的JAX-RS中,有@Consumes("application/json")