问题描述
我正在研究.NET Core 2 API项目,并且一直在尝试实现通用的字符串修剪模型绑定程序,该绑定程序将修剪提供的请求参数和字段值的所有字符串值.到目前为止,我取得了好坏参半的结果,并且正在努力寻找可以为我指明正确方向的可行例子.我一直在尝试实现与由Vikash Kumar发布.
I'm working on a .NET Core 2 API project and have been trying to implement a universal string trim model binder that would trim all string values of provided request parameters and field values. So far I have had mixed results and am struggling to find working example that would point me in the right direction. I've been trying to implement the same model binder as posted by Vikash Kumar.
此模型绑定程序适用于通过直接参数(例如 public IActionResult Profile(string username)
)传递给控制器操作的所有字符串值,但对于复杂对象中的字符串字段, TrimmingModelBinder
类的BindModelAsync 方法永远不会被调用.我的控制器中的HttpPost动作示例为 public IActionResult Profile([FormBody] ProfileLookupModel模型)
.模型联编程序似乎并未检查复杂模型的字段.对于作为字符串列表的字段,它也不起作用.
This model binder works fine for all string values that are passed into controller actions via direct parameters, such as public IActionResult Profile(string username)
, but for string fields in complex objects the BindModelAsync
method of the TrimmingModelBinder
class never gets called. An example of an HttpPost action in my controller would be public IActionResult Profile([FormBody] ProfileLookupModel model)
. The model binder does not seem to check the fields of the complex model. It also doesn't work for fields that are Lists of strings.
我记得在.NET Core之前,指定字符串修剪模型绑定程序将递归检查复杂模型的每个字段,甚至是复杂模型中的模型..NET Core中似乎并非如此,但我可能是错的.我的项目针对的是 netcoreapp2.0
框架.
I recall prior to .NET Core, specifying a string trim model binder would recursively check each field of complex models, even models within complex models. This doesn't seem to be the case in .NET Core, but I might be wrong. My project is targeting the netcoreapp2.0
framework.
我很好奇是否有人和我有同样的问题,并可能找到解决方案.
I'm curious if anyone has had the same issue as me and possibly found a solution for it.
注意:我尚未发布任何示例代码,因为它与所引用文章中的代码相同.
Note: I haven't posted any sample code as it is the same as the code from the referenced article.
推荐答案
我将在这里加2美分.我没有使用某种模型绑定挂钩,而是去了一个动作过滤器.优点之一是开发人员可以选择要使用的操作,而不是对所有请求和模型绑定进行此处理(不是这样做应该对性能产生太大影响).顺便说一句动作过滤器也可以全局应用.
I'll add my 2 cents here. Instead of using some kind of model binding hook, I went to a action filter. One of the advantages is that the developer can select which actions to use, instead of having this processing for all requests and model bindings (not that it should affect performance that much). Btw action filters can also be applied globally.
这是我的代码,首先创建一个动作过滤器.
Here is my code, first create an action filter.
public class TrimInputStringsAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
foreach (var arg in context.ActionArguments.ToList())
{
if (arg.Value is string)
{
if (arg.Value == null)
{
continue;
}
string val = arg.Value as string;
if (!string.IsNullOrEmpty(val))
{
context.ActionArguments[arg.Key] = val.Trim();
}
continue;
}
Type argType = arg.Value.GetType();
if (!argType.IsClass)
{
continue;
}
TrimAllStringsInObject(arg.Value, argType);
}
}
private void TrimAllStringsInObject(object arg, Type argType)
{
var stringProperties = argType.GetProperties()
.Where(p => p.PropertyType == typeof(string));
foreach (var stringProperty in stringProperties)
{
string currentValue = stringProperty.GetValue(arg, null) as string;
if (!string.IsNullOrEmpty(currentValue))
{
stringProperty.SetValue(arg, currentValue.Trim(), null);
}
}
}
}
要使用它,请注册为全局过滤器,或使用TrimInputStrings属性装饰您的操作.
To use it, either register as global filter or decorate your actions with the TrimInputStrings attribute.
[TrimInputStrings]
public IActionResult Register(RegisterViewModel registerModel)
{
// Some business logic...
return Ok();
}
这篇关于ASP .NET Core 2中的字符串修剪模型绑定程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!