Search code examples
c#asp.netasp.net-web-apiodataswagger

Odata Controller not working with multiple Keys


I work with an ASP.NET webapi with entity framework and odataV4 controller

First i had one [Key] tag in my model and every controllerAction was shown in the swagger UI

Now i have 3 [Key] tags in my model and only controllerActions with no arguments are shown in the swagger UI

I changed my Model from

    //Some other stuff
    [Key]
    public byte first{ get; set; }
    public int second{ get; set; }
    public long third{ get; set; }
    //Some other stuff

to

      //Some other stuff
    [Key]
    public byte first{ get; set; }
    [Key]
    public int second{ get; set; }
    [Key]
    public long third{ get; set; }
    //Some other stuff

and the controllerAction from

public async Task<IHttpActionResult> Put([FromODataUri] byte key, Delta<lists> patch)

to

public async Task<IHttpActionResult> Put([FromODataUri] byte first, [FromODataUri] int second, [FromODataUri] long third, Delta<lists> patch)

with one key the Endpoints are shown in the swagger UI and they work, with multiple keys the Endpoints are not shown in the swagger UI and everytime i try to reach the endpoint this error shows up

"No action was found on the controller 'lists' that matches the request."

I send a PUT Request with .../myController(first=1,second=1,third=10) in the URL

What i am missing or doing wrong?


Solution

  • Which version do you use?

    As I know, the latest 5.x and 6.x version do support the multiple keys, no matter attribute routing or convention routing you are using.

    See the comments in https://github.com/OData/WebApi/blob/maintenance-V4/src/System.Web.OData/OData/Routing/Conventions/ProcedureRoutingConventionHelpers.cs#L133-L137

    So for your scenario, if in convention routing, the parameter name in the method of controller should be prefixed with "key".

    public async Task<IHttpActionResult> Put([FromODataUri] byte keyfirst, [FromODataUri] int keysecond, [FromODataUri] long keythird, Delta<lists> patch)
    

    Hope it can help you. Thanks!