【发布时间】:2019-09-04 06:16:22
【问题描述】:
我已经尝试过this,但我不认为这是我的情况。 This 也不起作用。
我正在使用 ASP.NET Core 2 Web API。我刚刚创建了一个虚拟模型绑定器(现在它的作用无关紧要):
public class SanitizeModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var modelName = bindingContext.ModelName;
return Task.CompletedTask;
}
}
现在,我有一个模型。这个:
public class UserRegistrationInfo
{
public string Email { get; set; }
[ModelBinder(BinderType = typeof(SanitizeModelBinder))]
public string Password { get; set; }
}
还有一个动作方法:
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> RegisterAsync([FromBody] UserRegistrationInfo registrationInfo)
{
var validationResult = validateEmailPassword(registrationInfo.Email, registrationInfo.Password);
if (validationResult != null)
{
return validationResult;
}
var user = await _authenticationService.RegisterAsync(registrationInfo.Email, registrationInfo.Password);
if (user == null)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Couldn't save the user.");
}
else
{
return Ok(user);
}
}
如果我从客户端发出 post 请求,我的自定义模型绑定器不会被触发,并且在 action 方法中继续执行。
我尝试过的事情:
将ModelBinder 属性应用于整个模型对象:
[ModelBinder(BinderType = typeof(SanitizeModelBinder))]
public class UserRegistrationInfo
{
public string Email { get; set; }
public string Password { get; set; }
}
这可行,但对于整个对象,我不希望这样。我希望默认模型绑定器完成其工作,然后仅将我的自定义模型绑定器应用于某些属性。
我读到 here 是 FromBody 的错,所以我将它从操作方法中删除。它也不起作用。
我尝试在此处将ModelBinder 属性更改为BindProperty:
public class UserRegistrationInfo
{
public string Email { get; set; }
[BindProperty(BinderType = typeof(SanitizeModelBinder))]
public string Password { get; set; }
}
但它不起作用。
令人失望的是,本来应该很简单的事情变得相当繁琐,而且分散在几个博客和 github 问题上的信息根本没有帮助。所以,如果你能帮助我,那将不胜感激。
【问题讨论】:
-
有趣,也许你刚刚发现了一个错误。您的代码看起来正确 (docs.microsoft.com/en-us/aspnet/core/mvc/advanced/…)。在 MVC 存储库中创建问题可能会更好。您也可以尝试使用较新的版本来确定。
-
删除
[FromBody]后会发生什么?通过这样做,您实际上是从 JSON 正文切换到application/x-www-form-urlencoded正文,所以当您删除它时您是否也在更改正文的格式? -
@KirkLarkin 不,我刚刚删除了
[FromBody]并在操作方法中设置了一个断点。执行操作方法,但不执行自定义模型绑定器。 -
那么当你这样做时,
Email和Password有值吗?我想知道这种变化是否真的意味着什么都没有受到任何约束,因为您发送的 JSON 是预期的application/x-www-form-urlencoded(假设您正在使用 JSON)。 -
这确认您需要自定义
JsonConverter而不是自定义IModelBinder。当你说你有时,你并没有真正删除[FromBody],因为[ApiController]推断它。
标签: asp.net asp.net-core asp.net-core-2.0 asp.net-core-webapi custom-model-binder