I have a Bussiness Entity that is recognized by 2 Keys, for example:
class UserItem {
[Key]
[Column(Order = 1)]
public string UserId {get;set;}
[Key]
[Column(Order = 2)]
public string ItemName {get; set;}
public int Count {get; set;}
}
Now using ASP.NET Web Api, how can I make an HTTP GET or HTTP DELETE to accept multiple parameters? Currently, the default generated template only accept 1 key:
class ItemController : ApiController {
.....
//api/item/[key]
[HttpGet]
[ResponseType(typeof(UserItem))]
public async Task<IHttpActionResult> GetUserItem(string id)
{
UserItem item = await db.useritems.FindAsync(id);
......
}
......
}
db is my datacontext, i'm using EntityFramework 6 with ASP.NET Web Api 2
As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.
There's nothing wrong with using DELETE on a collection and filtering by query parameters.
Map your route like below will allow you to pass two parameter, you can add more that two parameter this way
[HttpGet]
[Route("api/item/{id1}/{id2}")]
[ResponseType(typeof(UserItem))]
public async Task<IHttpActionResult> GetUserItem(string id1, string id2)
{
UserItem item = await db.useritems.FindAsync(id1);
......
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With