【发布时间】:2018-06-01 17:16:25
【问题描述】:
我正在实现一个自定义参数绑定器,它继承 HttpParameterBinding 并将自定义行为应用于某些参数。在某些情况下,我不想应用自定义行为,在这种情况下,我想遵循默认情况下 Web API 所做的任何事情。这个决定将在 ExecuteBindingAsync 中做出。如何在 ExecuteBindingAsync 中实现此默认行为?
我相信这通常是通过简单地不应用参数绑定来完成的绑定到参数)。但是在我的情况下,我需要决定是否在运行时应用绑定,所以我需要在 ExecuteBindingAsync 中执行此操作。
我希望在我的自定义 HttpParameterBinding 类中执行以下操作:
public override async Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
{
if (IsCustomBindingNeeded()) {
// apply custom binding logic... call SetValue()... I'm good with this part
}
else {
// ***************************************************************
// This is where I want to use the default implementation.
// Maybe something like this (using a made up class name):
await return new WhateverDefaultParameterBinding().ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
// ...or at least be able to call GetValue() and get the correct value and then I can call SetValue()
// ***************************************************************
}
}
我尝试调用 GetValue(),但它总是返回 null。我假设需要执行一些额外的步骤,以便基类 (HttpParameterBinding) 可以创建值。
我的偏好是直接调用 .NET 框架中包含该默认逻辑的任何方法。我宁愿不必重复该逻辑。
【问题讨论】:
标签: .net asp.net-web-api asp.net-web-api2 parameterbinding