c# - 如何删除 ASP.NET Core 2.0 中错误消息的 ModelState 前缀?
问题描述
我正在开发一个将由我的客户使用的 ASP.NET Core 2.0 API。我遇到的一个问题是,当我使用 ModelState 对请求有效负载进行输入验证时,消费者看到的结果错误消息在响应 JSON 中有一个 [objectPrefix].PropertyName。我们的 API 文档列出了属性名称,但没有列出对象类,因此当消费者编写将 JSON 响应反序列化到其本地对象模型的代码时,前缀会产生问题。
我可以在 Service.AddMvc 的 Startup.cs ConfigureServices 方法或类似的方法中设置任何选项来禁用此前缀吗?
如果重要的话,我在我的 API、.NET Core 2.0.4 和 VS2016 v15.5.7 中使用 Microsoft.AspNetCore.All(2.0.7) 依赖项。
我正在使用 System.ComponentModel.DataAnnotations 库中的数据注释并装饰我的创建 DTO 类属性,如下所示;
[Required]
[MaxLength(14)]
public string AccountNumber
{
get => _accountNumber;
set => _accountNumber = !string.IsNullOrWhiteSpace(value) ? value.Trim() : string.Empty;
}
当消费者在请求载荷中没有提供帐号时,返回的错误如下所示;
{
"[AccountDto].AccountNumber": [
"The AccountNumber field is required."
]
}
我想要做的是消除[AccountDto]。前缀,以便错误 JSON 看起来像这样;
{
"AccountNumber": [
"The AccountNumber field is required."
]
}
我找到了这个SO帖子,但它似乎引用了旧的 ASP.NET。
目前,我让我的客户对 json 响应进行字符串替换,但我真的很想有一个更好的解决方案。
有任何想法吗?
2018 年 5 月 16 日更新
似乎前缀的问题与我在 *ForCreationDtos 中使用 Validate 方法有关。
例如,
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (CompanyId == 0)
{
yield return new ValidationResult("A Company ID is required.", new[] { "CompanyId" });
}
}
但是,我找到了一种解决方法,方法是使用全局 ModelState 处理程序并对其进行修改以解析出前缀。
public class ValidateModelAttribute : ActionFilterAttribute
{
/// <summary>
/// Validates model state upon action execution
/// </summary>
/// <param name="context">ActionExecutingContext object</param>
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.ModelState.IsValid) return;
var errorList = context.ModelState.Where(ms => ms.Value.Errors.Any()).ToDictionary(
kvp => kvp.Key.Replace("[0].", ""),
kvp => kvp.Value.Errors.Select(e => string.IsNullOrEmpty(e.ErrorMessage) ? e.Exception.Message : e.ErrorMessage).ToArray()
);
var globalErrorDto = new GlobalErrorDto { Errors = errorList };
context.Result = new BadRequestObjectResult(globalErrorDto);
}
}
这有点粗略,假设为“[0]”。作为前缀,但这是我在 DTO 类中实现 Validate 方法时得到的前缀。这似乎解决了我的具体问题。
解决方案
我正在使用Microsoft.AspNetCore.All
v2.0.8、 v2.0.7Microsoft.NETCore.App
和Visual Studio
Community 2017 v15.7.1,一切都如你所愿。
屏幕截图 #1:没有帐号 - 400
屏幕截图 #2:帐号太长 - 400
屏幕截图 #3:有效帐号 - 201
我无法重现您的问题。我什至想也许我只是在 Web 项目中创建了模型,所以我什至创建了一个单独的类项目来包含 DTO。它仍然像你想要的那样工作!
DTO
using System.ComponentModel.DataAnnotations;
namespace DL.SO.ModelState.Dto.Users
{
public class AccountModel
{
[Required]
[MaxLength(14)]
[Display(Name = "account number")]
public string AccountNumber { get; set; }
}
}
控制器
using DL.SO.ModelState.Dto.Users;
using Microsoft.AspNetCore.Mvc;
namespace DL.SO.ModelState.Controllers
{
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetById(string id)
{
// Just testing
return Ok(id);
}
[HttpPost]
public IActionResult Post(AccountModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Just testing so I pass in null
return CreatedAtAction(nameof(GetById),
new { id = model.AccountNumber }, null);
}
}
}
启动
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace DL.SO.ModelState
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}
推荐阅读
- c++ - 用 C++ 实现异步惰性生成器
- java - 我想在Java中通过rexgs在=之后提取字符串
- android - 根据节点日期查询 Firebase 数据
- aws-lambda - 如何让我的 AWS Lambda 函数中的 fabric8 k8s 客户端使用 EKS 正确进行身份验证?
- javascript - javascript中的异步函数不起作用
- ios - 导航栏在标签视图中转到另一个标签时不显示
- ruby-on-rails - 使用 CookieStore 在多个 Rails 微服务之间共享会话
- javascript - 将静态数据范围移动到另一个工作表,同时将其他字段放入目标旁边的列中(Google 表格)
- python - 按条件删除前面的行
- delphi - Delphi $E 指令