Home > Blockchain >  How to disable case sensitive for route variable in asp net core API?
How to disable case sensitive for route variable in asp net core API?

Time:03-08

I have this

[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();
}  

The {clientid} from Route won't bind to AddClientEmployeeCommand.ClientId unless I change it to {ClientId}. Is there any way to disable case-sensitive for this case?

CodePudding user response:

When you try to bind a class property with FromRoute it try to find a route section based on that property name and because the clientid is not equal with ClientId it won't bind. For solve this you should specify name for property like this:

 public class AddClientEmployeeCommand : IRequest<Unit>
        {
            [FromRoute(Name = "clientid")]
            public Guid ClientId { get; set; }
            [FromBody]
            public Employee Employee { get; set; } = new Employee();
        }

Also for preventing binding error in calling api you can specify type in the route

[Route("client/{clientid:guid}/employees")]
  • Related