asp.net-core - 运行时类型的 ASP.NET Core ModelBinding
问题描述
如何编写将基于查询字符串创建模型实例的活页夹
public class RpcBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
string modelName = bindingContext.ModelName;
ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
// My own method to get assembly type
if (!TryGetRequestType(valueProviderResult.FirstValue, out Type requestType))
return Task.CompletedTask;
object modelInstance = Activator.CreateInstance(requestType);
// ???? Call [FromBody] for modelInstance
bindingContext.Result = ModelBindingResult.Success(modelInstance);
return Task.CompletedTask;
}
}
[HttpPost("rpc/{request:required}")]
public async Task<IActionResult> InvokeAsync([ModelBinder(typeof(RpcBinder))]object request, CancellationToken token)
{
...
}
并由默认提供者从请求正文中填充此实例
解决方案
我的解决方案:
public class RpcBinder : IModelBinder
{
private readonly MvcOptions _options;
private readonly IHttpRequestStreamReaderFactory _readerFactory;
private readonly ILoggerFactory _loggerFactory;
public RpcBinder(IOptions<MvcOptions> options,
IHttpRequestStreamReaderFactory readerFactory,
ILoggerFactory loggerFactory)
{
_options = options.Value;
_readerFactory = readerFactory;
_loggerFactory = loggerFactory;
}
publicTask BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
string modelName = bindingContext.ModelName;
ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
// Get model Type from Assembly by your own realization
if (valueProviderResult == ValueProviderResult.None
|| !TryGetRequestType(valueProviderResult.FirstValue, out Type requestType))
return Task.CompletedTask;
bindingContext.ModelMetadata = bindingContext.ModelMetadata.GetMetadataForType(requestType);
var bodyBinder = new BodyModelBinder(_options.InputFormatters, _readerFactory, _loggerFactory, _options);
return bodyBinder.BindModelAsync(bindingContext);
}
}
推荐阅读
- ms-access - 编辑用户 - 毫秒访问
- angularjs - 带过滤器的 ng-show 首先返回 true,然后是过滤器的结果
- javascript - 创建按钮搜索过滤器
- python - X_set[y_set == j, 0] 是什么意思?
- spring - @ContextConfiguration 和安全链过滤器
- c# - 无法加载类型“BaseClass.aspx.cs”
- terraform - 如何在 terraform 资源中创建循环?
- angular - angular 6 service worker dataGroups 不工作
- sql - SQL - 根据列值使用不同的选择语句
- node.js - 无限循环卡夫卡消费者