【问题标题】:How to use a parameter in Web API v2?如何在 Web API v2 中使用参数?
【发布时间】:2019-02-02 10:23:06
【问题描述】:

我不知道从哪里开始。我之前问过一个问题,有人建议我看看属性路由。我阅读了它,虽然它帮助我创建了下面的代码,但我仍然不确定如何像我想要的那样限制它。

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

当我用谷歌搜索它时,我会想到这个问题:Multiple actions were found that match the request in Web Api

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

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

【问题讨论】:

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


    【解决方案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。 url IS 中的“2”是您在[Route("api/getFoo/{id}")] 属性中标记的{id} 属性。

    我还修改了你的代码:

    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


    一开始你可能很难理解,但你最终会习惯的。这就是将参数传递给控制器​​操作参数的方式。

    【讨论】:

    • 对,这正是我的示例代码中的内容。我只是不确定...{ }s 之间的内容。我有一个可以返回所有评论的有效 API,还有一个可以根据评论 ID 限制评论的 API。我正在努力让它返回基于 SiteID 而不是模型类中的“键”的所有评论。
    • @phroureo 我更新了我的答案。基本上,您需要使用 LINQ 来指定您只需要 Id 属性等于传入的 siteId 属性的评论。你可以这样做db.Reviews.Where(review =&gt; review.Id == siteId)
    • 谢谢!这就是我一直在寻找的。 :)
    • @phroureo 我很高兴听到这个消息。感谢您的投票和选择的答案!
    • 好吧,我撒谎了。出于某种原因,.../api/review/site?id=1 返回与/api/review/1 相同的内容。它根本不看siteID
    【解决方案2】:

    如果要获取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
    

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

    打电话

      /abc/123?IdSite=17
    

    返回

      17
    

    【讨论】:

    • 抱歉,您的回答对我来说并不完全清楚。我会在您的代码中添加什么,以便它知道在 Review 控制器中查找 SiteID 与参数匹配的评论?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多