简体   繁体   中英

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?

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

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 ;)

URI path cannot have a query string in it so it is not allowed, refer - http://tools.ietf.org/html/rfc3986#section-3.3

For your answer what you want is -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM