实现搜索不在web api中工作

时间:2014-09-10 10:06:52

标签: asp.net-mvc rest asp.net-web-api

使用我们的移动应用的网络API,我们似乎无法让我们的搜索方法工作。 当我们尝试从api调用时,我们的搜索方法得到以下结果

 <Error>
 <Message>
 No HTTP resource was found that matches the request URI 'https://hippokros-     api.azurewebsites.net/api/Recommends/GetCardRecommendsByFriendId/100000097477513'.
 </Message>
 <MessageDetail>
 No action was found on the controller 'Recommends' that matches the request.
 </MessageDetail>
 </Error>

我们在Fiddler中收到502 HTTP请求,但是当我们调用GetRecommends / 1并传入id时,我们得到了结果。

控制器代码是否很短,所以我只显示所有内容

 public class RecommendsController : ApiController
{
    private mtgServiceEntities db = new mtgServiceEntities();

    // GET: api/Recommends
    [HttpGet]
    public IQueryable<Recommend> GetRecommends()
    {
        return db.Recommends;
    }

    // GET: api/Recommends/5
    [ResponseType(typeof(Recommend))]
    [HttpGet]
    public IHttpActionResult GetRecommend(int id)
    {
        Recommend recommend = db.Recommends.Find(id);

        if (recommend == null)
        {
            return NotFound();
        }

        return Ok(recommend);
    }

    // GET: api/Recommends/5
    [ResponseType(typeof(Recommend))]
    [HttpGet]
    public IHttpActionResult GetRecommend(string cardname)
    {
        List<Recommend> recommend = db.Recommends.Where(x => x.card_name == cardname).ToList();

        if (recommend == null)
        {
            return NotFound();
        }

        return Ok(recommend);
    }

    //GET: api/Recommend/Search/cardname
    [ResponseType(typeof(Recommend))]
    [HttpGet]
    public IHttpActionResult GetFriendRecommendsByCardName(string cardname)
    {
        List<Recommend> recommends = db.Recommends.Where(x => x.card_name == cardname).ToList();
        return Ok(recommends);
    }

    [ResponseType(typeof(Recommend))]
    [HttpGet]
    public IHttpActionResult GetCardRecommendsByFriendId(string friendId)
    {
        List<Recommend> recommends = db.Recommends.Where(x => x.facebookId == friendId).ToList();
        return Ok(recommends);
    }

    // PUT: api/Recommends/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutRecommend(int id, Recommend recommend)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != recommend.ID)
        {
            return BadRequest();
        }

        db.Entry(recommend).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!RecommendExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/Recommends
    [ResponseType(typeof(Recommend))]
    public IHttpActionResult PostRecommend(Recommend recommend)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Recommends.Add(recommend);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException)
        {
            if (RecommendExists(recommend.ID))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtRoute("DefaultApi", new { id = recommend.ID }, recommend);
    }

    // DELETE: api/Recommends/5
    [ResponseType(typeof(Recommend))]
    public IHttpActionResult DeleteRecommend(int id)
    {
        Recommend recommend = db.Recommends.Find(id);
        if (recommend == null)
        {
            return NotFound();
        }

        db.Recommends.Remove(recommend);
        db.SaveChanges();

        return Ok(recommend);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool RecommendExists(int id)
    {
        return db.Recommends.Count(e => e.ID == id) > 0;
    }
}

我们在webapiconfig文件中的路由如下

public static void Register(HttpConfiguration config)
    {
        //Web API configuration and services
        config.EnableCors();

        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

我们不确定我们错误的设置或配置,但基本上它告诉我们当我们知道我们有正确的网址并且我们使用https来调用它时没有任何内容。

请帮忙!

1 个答案:

答案 0 :(得分:1)

我使用属性路由,因为我可以很容易地看到映射到什么的:)我建议阅读this article以更好地理解Web API属性路由。

要匹配您的地址,您可以尝试以下代码:

[RoutePrefix("api/recommends")]
public class RecommendsController : ApiController
{
    ...

    Route("GetCardRecommendsByFriendId/{friendId}")]
    [ResponseType(typeof(Recommend))]
    [HttpGet]
    public IHttpActionResult GetCardRecommendsByFriendId(string friendId)
    {
       List<Recommend> recommends = db.Recommends.Where(x => x.facebookId == friendId).ToList();
       return Ok(recommends);
    }        
}