0

I have following API endpoint

public class CreateSupplierCommand
{
    public string Name { get; set; } = null!;
    public string? Website { get; set; }
}
    
[ApiController]
public class SuppliersController : ControllerBase
{
    // ...

    [HttpPost, Route("api/supplier")]
    public IActionResult Post([FromBody] CreateSupplierCommand command)
    {
        var supplier = new Supplier(command.Name, command.Website);
        _context.Suppliers.Add(supplier);
        _context.SaveChanges();
        return Ok();
    }
}

and the following request payload with PascalCase works. The endpoint is called and supplier is created.

{Name: "foo", Website: "bar"}

but camelCase attributes does not. The endpoint is not even called

{name: "foo", website: "bar"}

I am not sure, but I think in .NET6 it worked. Any idea?

1 Answer 1

0

It seems that following code in Program.cs fix the issue

builder.Services.AddControllersWithViews().AddJsonOptions(options =>
{
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});

I found answer here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.