I'm writing a controller to get the Books with for certain authors with a specific rating. This is the code for the GET method :
public IQueryable<Book> GetBooks(string context)
{
string rating = context.Split('?')[0];
string[] authors = context.context.Split('?')[1].Split(',');
return db.Books.Where(s => authors.Contains(s.AuthorName) && s.Rating == rating);
}
When I run the project and enter this URL :
http://localhost:65239/api/books/5?James,Tom
I get this error:
<Error> <Message> The requested resource does not support http method 'GET'. </Message> </Error>
What am I doing wrong?
Try this;
public JsonResult GetBooks(string context)
{
string rating = context.Split('?')[0];
string[] authors = context.context.Split('?')[1].Split(',');
var books = db.Books.Where(s => authors.Contains(s.AuthorName) && s.Rating == rating);
return Json(books, JsonRequestBehavior.AllowGet);
}
原来我使用了错误的URL格式。
http://localhost:65239/api/books?context=5?James,Tom
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.