如何在 asp net core API 中禁用路由变量的大小写敏感?
How to disable case sensitive for route variable in asp net core API?
我有这个
[HttpPost]
[Route("client/{clientid}/employees")]
[SwaggerOperation(Tags = new[] { "Client" })]
public async Task<Unit> AddClientEmployee(AddClientEmployeeCommand request)
{
return await _mediator.Send(request);
}
public class AddClientEmployeeCommand : IRequest<Unit>
{
[FromRoute]
public Guid ClientId { get; set; }
[FromBody]
public Employee Employee { get; set; } = new Employee();
}
来自 Route
的 {clientid}
不会绑定到 AddClientEmployeeCommand.ClientId
,除非我将其更改为 {ClientId}
。对于这种情况,有什么办法可以禁用区分大小写吗?
当您尝试将 class 属性 与 FromRoute
绑定时,它会尝试根据该 属性 名称查找路线部分,因为 clientid
不等于 ClientId
它不会绑定。为了解决这个问题,您应该像这样为 属性 指定名称:
public class AddClientEmployeeCommand : IRequest<Unit>
{
[FromRoute(Name = "clientid")]
public Guid ClientId { get; set; }
[FromBody]
public Employee Employee { get; set; } = new Employee();
}
另外,为了防止调用时出现绑定错误api,您可以在路由中指定类型
[Route("client/{clientid:guid}/employees")]
我有这个
[HttpPost]
[Route("client/{clientid}/employees")]
[SwaggerOperation(Tags = new[] { "Client" })]
public async Task<Unit> AddClientEmployee(AddClientEmployeeCommand request)
{
return await _mediator.Send(request);
}
public class AddClientEmployeeCommand : IRequest<Unit>
{
[FromRoute]
public Guid ClientId { get; set; }
[FromBody]
public Employee Employee { get; set; } = new Employee();
}
来自 Route
的 {clientid}
不会绑定到 AddClientEmployeeCommand.ClientId
,除非我将其更改为 {ClientId}
。对于这种情况,有什么办法可以禁用区分大小写吗?
当您尝试将 class 属性 与 FromRoute
绑定时,它会尝试根据该 属性 名称查找路线部分,因为 clientid
不等于 ClientId
它不会绑定。为了解决这个问题,您应该像这样为 属性 指定名称:
public class AddClientEmployeeCommand : IRequest<Unit>
{
[FromRoute(Name = "clientid")]
public Guid ClientId { get; set; }
[FromBody]
public Employee Employee { get; set; } = new Employee();
}
另外,为了防止调用时出现绑定错误api,您可以在路由中指定类型
[Route("client/{clientid:guid}/employees")]