GET :http://www.Example.com/Api/1/0/Book/Company/0
[Route("{UserId}/{Category}/books/{BookType}/{Page}")]
[HttpGet]
[RequestAuthorization]
public Response Get(int UserId,string Category, string BookType,int Page )
{
var books= this.contentService.GetUserItems(UserId,Category, BookType, Page)
return new Response() { Status = ApiStatusCode.Ok, Books = books};
}
上面的代码对我来说效果很好。
我的问题是是否可以在 GET 请求中绑定(bind)请求模型?
例如我有这样一个请求模型
public class BookbRequestModel
{
public int UserId { get; set; }
public int Category { get; set; }
public int Page { get; set; }
public string BookType { get; set; }
}
我想要这样的 get 请求
GET :http://www.Example.com/Api/1/0/Book/Company/0
to bind the data to my request model
[Route("{UserId}/{Category}/books/{BookType}/{Page}")]
[HttpGet]
[RequestAuthorization]
public Response Get(BookbRequestModel book )
{
var books= this.contentService.GetUserItems(book.UserId,book.Category,book.BookType,book.Page)
return new Response() { Status = ApiStatusCode.Ok, Books = books};
}
我试过了,但每次我在我的书中得到 null (BookRequestModel)
最佳答案
添加 [FromUri] 然后重试如下
[Route("{UserId}/{Category}/books/{BookType}/{Page}")]
[HttpGet]
[RequestAuthorization]
public Response Get(([FromUri] BookbRequestModel book )
{
var books= this.contentService.GetUserItems(book.UserId,book.Category,book.BookType,book.Page)
return new Response() { Status = ApiStatusCode.Ok, Books = books};
}
更多信息:-
http://www.c-sharpcorner.com/UploadFile/2b481f/parameter-binding-in-Asp-Net-web-api/
关于c# - 如何使用路由属性绑定(bind) WebAPI GET 请求中的请求模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075892/