【发布时间】:2021-03-29 22:14:09
【问题描述】:
我有一个第三方专有应用程序,我需要在我的 ASP.NET Core 5.0 Web API 应用程序中为此编写一个 API 端点。
第三方应用程序发出一个 HTTP post 请求,请求正文中只有二进制数据,以及内容类型 application/x-www-form-urlencoded 或有时是 application/octet-stream(有点随机,但数据相同)。
我的动作处理程序如下所示:
[Route("~/Validation")]
[ApiController]
public class ValidationController : ControllerBase
{
[HttpPost("{requestId}")]
[Consumes(@"application/octet-stream", @"application/x-www-form-urlencoded")]
[Produces(@"application/octet-stream")]
public async Task<IActionResult> Validation_Post([FromRoute] string requestId)
{
byte[] rawRequestBody = Array.Empty<byte>();
{
long streamInitialPos = 0;
if (Request.Body.CanSeek) // rewind for this read.
{
streamInitialPos = Request.Body.Position;
Request.Body.Seek(0, SeekOrigin.Begin);
}
using (var ms = new MemoryStream())
{
await Request.Body.CopyToAsync(ms);
rawRequestBody = ms.ToArray() ?? throw new NullReferenceException();
}
if (Request.Body.CanSeek) // rewind to initial position.
Request.Body.Seek(streamInitialPos, SeekOrigin.Begin);
}
// TODO: Handle rawRequestBody data.
return new FileContentResult(new byte[] { 1 }, @"application/octet-stream")
{
EnableRangeProcessing = true,
LastModified = DateTime.UtcNow
};
}
当第三方应用程序将其 HTTP 发布请求发送到我的 API 端点时,我的 API 应用程序崩溃并显示 System.ArgumentException:
Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Error: Connection ID "18374686481282236432", Request ID "80000011-0000-ff00-b63f-84710c7967bb": An unhandled exception was thrown by the application.
System.ArgumentException: The key '[omitted binary data]' is invalid JQuery syntax because it is missing a closing bracket. (Parameter 'key')
at Microsoft.AspNetCore.Mvc.ModelBinding.JQueryKeyValuePairNormalizer.NormalizeJQueryToMvc(StringBuilder builder, String key)
at Microsoft.AspNetCore.Mvc.ModelBinding.JQueryKeyValuePairNormalizer.GetValues(IEnumerable`1 originalValues, Int32 valueCount)
at Microsoft.AspNetCore.Mvc.ModelBinding.JQueryFormValueProviderFactory.AddValueProviderAsync(ValueProviderFactoryContext context)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList`1 factories)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.TryCreateAsync(ActionContext actionContext, IList`1 factories)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished HTTP/1.1 POST http://localhost:10891/validation/dummy application/x-www-form-urlencoded 11072 - 500 - - 164.0024ms
日志显示正在使用正确的路由操作。
如何仅为此特定操作处理程序禁用自动模型绑定?
提醒:我无法对第三方应用程序进行任何更改。我必须处理我收到的东西。我知道请求内容类型错误。请不要在这方面做任何笔记。
编辑:我已经找到了这个错误的表面原因。当我从函数签名中删除[FromRoute] string requestId 时,不会发生错误。当我重新引入它时,错误再次出现。
不起作用(导致 ASP.NET Core 内部异常):
public async Task<IActionResult> Validation_Post([FromRoute] string requestId)
有效:
public async Task<IActionResult> Validation_Post()
但是,我需要通过Request.RouteValues["requestId"]访问路由变量。
无论如何,问题仍然存在: 如何仅为此特定操作处理程序禁用自动模型绑定?
【问题讨论】:
-
ArgumentException是哪一行?stream来自哪里? -
@mxmissile:感谢您指出
stream,我已经大大减少了代码,以至于我在代码sn-p中引入了错误。动作源代码现已修复,我在问题中添加了完整的堆栈跟踪。 -
@mxmissile:而且没有线。我猜它是 ASP.NET Core 内部的(框架)。
-
@RoarS.:
~指的是根 mount 点(与/的实际根路径相反)。删除[ApiController]没有帮助,请参阅我的问题更新。 -
@mxmissile:ASP.NET Core 模型内部绑定类包括对特定 jQuery 字段命名约定的特殊处理。看一下NormalizeJQueryToMvc()方法,它负责抛出这个异常。这与数据绑定期间字段如何映射到对象有关。此类查找 JQuery 语法并将其规范化以使用 MVC 语法,以便模型绑定的其余部分不需要考虑差异。
标签: c# asp.net-core asp.net-core-webapi model-binding asp.net-core-5.0