Search code examples
c#asp.net-web-apiasp.net-web-api-routing

Error with WebApi RouteAttribute


This is my prefix attribute

 [RoutePrefix("news/farms")]
 public class farmsController : ApiController

I want to delete a row based on farmid but i want a url structure like this

/news/farms/{farmId}?version={Version}

I tried route url like below code

 [HttpDelete, Route("{farmId}?version={Version}", Name = "Deletefarm")]

But it shows

The route template cannot start with a '/' or '~' character and it cannot contain a '?' character.

Please anyone let me know is there any other way to solve this error?


Solution

  • Set the RoutePrefix like in your example:

    [RoutePrefix("news/farms")]
    public class FarmsController : ApiController
    

    And your delete Methode:

    [HttpDelete]
    [Route("{farmId}")]
    public void Delete(int farmId, string version)
    

    Then this should work:

    .../news/farms/1?version=myVersion