如何在Web API v2中使用参数?

时间:2018-08-27 20:49:44

标签: c# asp.net-web-api

我不知道从哪里开始。我之前曾问过一个问题,有人建议我看一下属性路由。我仔细阅读了一下,尽管它帮助我创建了以下代码,但我仍然不确定如何按照自己的意愿来限制它。

public class ReviewController : ApiController
{
    private Review db = new Review();
    ////This GET works. It was auto-generated by Visual Studio. 
    // GET: api/Review
    public IQueryable<Review> GetReview()
    {
        return db.Review;
    }

    ////This is the GET that I'm trying to write, but don't know what to do
    // GET: api
    [Route("api/review/site")]
    [HttpGet]
    public IEnumerable<Review> FindStoreBySite(int SiteID)
    {
         return db.Review
    }

    ////This GET also works and was generated by VS. 
    // GET: api/Review/5
    [ResponseType(typeof(Review))]
    public IHttpActionResult GetReview(int id)
    {
        Review review = db.Review.Find(id);
        if (review == null)
        {
            return NotFound();
        }

        return Ok(review);
    }

基本上,我的目标是将API返回的内容限制为仅SiteID等于传递给URL的任何值的结果。我什至不知道从哪里开始,并且谷歌搜索/搜索“如何在Web api返回中放入内容”的堆栈溢出是徒劳的。

如何根据ReviewID之外的参数告诉API我想要返回什么?

编辑:我已经按照下面答案中的建议更新了代码,但是现在我遇到了一个新错误。

这是当前代码:

    private ReviewAPIModel db = new ReviewAPIModel();

    // GET: api/Review
    [Route("api/Review")]
    [HttpGet]
    public IQueryable<Review> GetReview()
    {
        return db.Review;
    }

    // GET: api
    [Route("api/Review/site/{siteid}")]
    [HttpGet]
    public IEnumerable<Review> FindStoreBySite(int siteid)
    {
        return db.Review.Where(Review => Review.SiteID == siteid);
    }

    // GET: api/Review/5
    [ResponseType(typeof(Review))]
    public IHttpActionResult GetReview(int id)
    {
        Review review = db.Review.Find(id);
        if (review == null)
        {
            return NotFound();
        }

        return Ok(review);
    }
}

这是我得到的错误:

Multiple actions were found that match the request

当我用google搜索它时,我会想到以下问题:Multiple actions were found that match the request in Web Api

但是,我在那里尝试了答案(我确认我正在使用Web API V2,并且我的webapiconfig.cs文件包含config.MapHttpAttributeRoutes();行。

此外,如您在上面的代码中所见,我包括了适当的路由。但是,我仍然收到一条错误消息,告诉我它正在返回两个相互冲突的API调用。

2 个答案:

答案 0 :(得分:1)

如果要获取GET的参数,这就像一个简单的重载,但是如果完成,则POST与[fromBody]一起使用,因为URL在标记[Route ("/abc/123/{id}")]

示例

代码

[Route ("/abc/123/{idSite}")]
[HttpGet]
public HttpResponseMessage ControllerIdSite(int IdSite){
   //CODE . . .
   return Request.CreateResponse<int>(HttpStatusCode.OK, IdSite);  
}

致电

  /abc/123/17

返回

  17

OR

[Route ("/abc/123")]
[HttpGet]
public HttpResponseMessage ControllerIdSite(int IdSite){
       //CODE . . .
       return Request.CreateResponse<int>(HttpStatusCode.OK, IdSite);  
}

致电

  /abc/123?IdSite=17

返回

  17

答案 1 :(得分:1)

要将参数传递给WebApi控制器,您需要向该控制器添加[Route()]属性,并为此{}标记链接中用作属性的部分。

要返回仅与传入参数匹配的评论,您需要使用LINQ来过滤数据。

这里是一个例子:

[Route("api/getFoo/{id}")]
public IHttpActionResult GetFoo(int id)
{
    return db.Foo.Where(x => x.Id == id);
}

字符串的{id}部分表示将在浏览器的URL中的ID:http://localhost:51361/api/getFoo/2。网址 IS 中在您的{id}属性中标记的[Route("api/getFoo/{id}")]属性中的“ 2 ”。

我还修改了您的代码:

public class ReviewController : ApiController
{
    ...


    [Route("api/review/site/{siteId}")]
    [HttpGet]
    public IEnumerable<Review> FindStoreBySite(int SiteID)
    {
         return db.Review.Where(review => review.Id == SiteID);
    }

    ...

您的请求网址应如下所示:http://localhost:51361/api/review/site?SiteID=2


一开始很难把头缠起来,但最终您会习惯的。这是将参数传递给Controller Action参数的方式。