r.vengadesh
r.vengadesh

Reputation: 1837

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?

Upvotes: 1

Views: 158

Answers (3)

PinBack
PinBack

Reputation: 2564

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

Upvotes: 2

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

URI path cannot have a query string in it so it is not allowed, refer - https://www.rfc-editor.org/rfc/rfc3986#section-3.3

For your answer what you want is -

[HttpDelete]
public IActionResult DeleteFarm(int farmId, [FromQuery] string version)
{
    //
}

Upvotes: 0

Roelant M
Roelant M

Reputation: 1706

why would you try to add the version in the route as a query param? Is this not something you want in your controller routing?

nevertheless: will this work?

[HttpDelete]
public async Task<IActionResult> Delete(int id, [FromQuery] string version)
{
 //...your code
}

with the [FromQuery] you specify that you want it from the.... query ;)

Upvotes: 0

Related Questions