如何禁用特定 ASP.NET Core 5.0 Web API 操作的自动模型绑定?
How do I disable automatic model binding for a specific ASP.NET Core 5.0 Web API action?
我有一个第三方专有应用程序,为此我需要在我的 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 post 请求发送到我的 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 核心内部异常):
public async Task<IActionResult> Validation_Post([FromRoute] string requestId)
有效:
public async Task<IActionResult> Validation_Post()
但是,我需要通过Request.RouteValues["requestId"]
访问路由变量。
不管怎样,问题依旧:
如何仅针对此特定操作处理程序禁用自动模型绑定?
我认为您实际上已经想出了解决方案——或者,至少,确定了所有关键组件。请允许我逐步介绍它们,以便您可以 assemble 将它们转化为解决方案。
禁用模型绑定
模型绑定仅在您的操作有参数时发生——否则没有模型可以绑定。这可能是一个实际的模型,比如 POCO,或者只是一个值类型,比如你在这里的字符串参数;相同的基本过程适用于两者。这就是为什么您在删除参数时没有收到错误消息,也是为什么您已经有效地回答了您的问题。
在没有模型绑定的情况下提取路由值
拥有参数的唯一目的或好处是让它参与模型绑定。所以如果你想禁用模型绑定,没有理由维护这个参数。没有它,您仍然可以使用 Request
属性 来提取请求值,包括表单字段、查询字符串参数、路由变量、请求 headers 等。因此,在这种情况下,如您所述,您仍然可以在操作中调用例如 Request.RouteValues["requestId"]
。直接调用 RouteValueDictionary
不会触发对 JQueryKeyValuePairNormalizer
class 的调用,因此您不会遇到相同的异常。
潜在的路由歧义
也就是说,根据您的路由和路由参数的定义方式,您可能遇到问题,例如,依赖此参数来消除您的其他重载之间的歧义行动,因此值得关注。
根本问题
至 return 至核心错误,但是,我还建议评估有关失败请求的请求数据,以确保没有任何可能与包含格式错误的索引器的字段名称混淆的内容。根据您的描述,我不希望出现这种情况,除非它以某种方式尝试将二进制数据解析为表单数据预期的 key/value 对,这可能是由于错误的内容类型。无论如何,the line of code that’s throwing the error 会发生,具体来说,当有一个字段包含 [
而没有相应的 ]
时。例如,它应该与以下查询字符串一起出现:
?key[0=5
如果您的传入请求经常出现类似的问题,那很可能就是罪魁祸首。显然,由于您无法控制客户端,您对此无能为力,但隔离起来很有用,这样您就可以为将来的实施提供指导。
也许这可以禁用模型绑定?
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
var factories = context.ValueProviderFactories;
factories.RemoveType<FormValueProviderFactory>();
factories.RemoveType<FormFileValueProviderFactory>();
factories.RemoveType<JQueryFormValueProviderFactory>();
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
我有一个第三方专有应用程序,为此我需要在我的 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 post 请求发送到我的 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 核心内部异常):
public async Task<IActionResult> Validation_Post([FromRoute] string requestId)
有效:
public async Task<IActionResult> Validation_Post()
但是,我需要通过Request.RouteValues["requestId"]
访问路由变量。
不管怎样,问题依旧: 如何仅针对此特定操作处理程序禁用自动模型绑定?
我认为您实际上已经想出了解决方案——或者,至少,确定了所有关键组件。请允许我逐步介绍它们,以便您可以 assemble 将它们转化为解决方案。
禁用模型绑定
模型绑定仅在您的操作有参数时发生——否则没有模型可以绑定。这可能是一个实际的模型,比如 POCO,或者只是一个值类型,比如你在这里的字符串参数;相同的基本过程适用于两者。这就是为什么您在删除参数时没有收到错误消息,也是为什么您已经有效地回答了您的问题。
在没有模型绑定的情况下提取路由值
拥有参数的唯一目的或好处是让它参与模型绑定。所以如果你想禁用模型绑定,没有理由维护这个参数。没有它,您仍然可以使用 Request
属性 来提取请求值,包括表单字段、查询字符串参数、路由变量、请求 headers 等。因此,在这种情况下,如您所述,您仍然可以在操作中调用例如 Request.RouteValues["requestId"]
。直接调用 RouteValueDictionary
不会触发对 JQueryKeyValuePairNormalizer
class 的调用,因此您不会遇到相同的异常。
潜在的路由歧义
也就是说,根据您的路由和路由参数的定义方式,您可能遇到问题,例如,依赖此参数来消除您的其他重载之间的歧义行动,因此值得关注。
根本问题
至 return 至核心错误,但是,我还建议评估有关失败请求的请求数据,以确保没有任何可能与包含格式错误的索引器的字段名称混淆的内容。根据您的描述,我不希望出现这种情况,除非它以某种方式尝试将二进制数据解析为表单数据预期的 key/value 对,这可能是由于错误的内容类型。无论如何,the line of code that’s throwing the error 会发生,具体来说,当有一个字段包含 [
而没有相应的 ]
时。例如,它应该与以下查询字符串一起出现:
?key[0=5
如果您的传入请求经常出现类似的问题,那很可能就是罪魁祸首。显然,由于您无法控制客户端,您对此无能为力,但隔离起来很有用,这样您就可以为将来的实施提供指导。
也许这可以禁用模型绑定?
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
var factories = context.ValueProviderFactories;
factories.RemoveType<FormValueProviderFactory>();
factories.RemoveType<FormFileValueProviderFactory>();
factories.RemoveType<JQueryFormValueProviderFactory>();
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}