.NET Core自定义模型联编程序调用默认模型联编程序 [英] .NET Core Custom model binder call default model binder
本文介绍了.NET Core自定义模型联编程序调用默认模型联编程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在.NET Core中实现一些中间件,该中间件将小数点后舍入到2个小数位.通过ComplexTypeModelBinder
,所有其他映射都可以像当前一样工作.我曾尝试在我的绑定器或从其继承之前调用该绑定器,但最终导致模型在碰到控制器时只是为空.
I'm trying to implement some middleware in .NET Core that rounds decimals to 2 decimal places. All of the other mapping can work as it currently does via the ComplexTypeModelBinder
. I've tried calling that binder before mine or inheriting from it, but it ends up with the model just being null when it hits the controller.
基本上,我所追求的功能与此处要求的相同:从自定义模型绑定程序调用默认模型绑定程序吗?,但适用于.NET Core.
Essentially I'm after the same functionality as asked here: Call Default Model Binder from a Custom Model Binder?, but for .NET core.
推荐答案
public class JqGridModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var request = bindingContext?.ActionContext?.HttpContext?.Request;
var param = new JqGridParam
{
isSearch = bool.Parse(request.Query["_search"]),
pageIndex = int.Parse(request.Query["page"]),
pageSize = int.Parse(request.Query["rows"]),
sortColumn = request.Query["sidx"].ToString(),
sortOrder = request.Query["sord"].ToString(),
id = request.Query["id"].ToString(),
param = request.Query["oper"].ToString(),
editOper = request.Query["edit"].ToString(),
addOper = request.Query["add"].ToString(),
delOper = request.Query["del"].ToString(),
@where = JqGridFilter.Create(request.Query["filters"]),
//operation = (OPERATION)System.Enum.Parse(typeof(OPERATION), request.Query["oper"]=="null" ? "none": request.Query["oper"].ToString())
};
bindingContext.Result = ModelBindingResult.Success(param);
return Task.CompletedTask;
}
}
这篇关于.NET Core自定义模型联编程序调用默认模型联编程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文