在c#中从模型中删除所需的验证



这可能是一个非常愚蠢的问题。还是要打消疑虑......

我想更新数据库中的密码,我们使用一个通用的模型来注册和更新

ie

{
"email": "string",
"password": "string",
"username": "string",
"name": "string",
"usertype": "string",
"professional": "string",
"securityQuestion": "string",
"securityAnswer": "string"
}

但我不想从json中发送每个节点我只想发送

我的端点是这样的

[HttpPatch]
[Route("/forgotpassword")]
public IActionResult ResetPassword( UserCredentialsDto credentials)
{
if (_authenticationManager.ResetPasword(credentials))
{
string success = "Password Has been set succcessfully";
return Ok(DataWrapperService.WrapData(success, true));
}
else {
string error = "invalid Credentials ";
return Unauthorized(DataWrapperService.WrapData(error, false));
}
}
public class UserCredentialsDto
{
public string Email { get; set; }
public string Password { get; set; }
public string Username { get; set; }
public string Name { get; set; }
public string Usertype { get; set; }
public string Professional { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
}

添加模型

Curl请求

curl -X 'PATCH' 
'https://localhost:7038/forgotpassword' 
-H 'accept: */*' 
-H 'Content-Type: application/json' 
-d '{
"email": "string",
"password": "string"
}'

Reposne

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-b161f3fe7b8c6e10713dab990c1a6928-fabfe562c3c83a8a-00",
"errors": {
"Name": [
"The Name field is required."
],
"Username": [
"The Username field is required."
],
"Usertype": [
"The Usertype field is required."
],
"Professional": [
"The Professional field is required."
],
"SecurityAnswer": [
"The SecurityAnswer field is required."
],
"SecurityQuestion": [
"The SecurityQuestion field is required."
]
}
}

任何帮助都是有帮助的,我使用。net 6,没有对模型

应用验证。

有两种解决方案:

  1. 将可空属性的类型修改为"string?">

  2. 设置如下:

    builder.Services.AddController(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
    

您可以阅读相关文档:https://learn.microsoft.com/en us/aspnet/core/mvc/models/validation?view=aspnetcore - 6.0 # non-nullable-reference-types-and-the-required-attribute

相关内容

  • 没有找到相关文章

最新更新