0
在我們的移動應用程序的網頁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>
API調用我們提琴手得到一個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 }
);
}
我們不知道設置或配置我們有錯,但基本上它告訴我們,當我們知道我們有正確的URL並且我們使用https來調用它時,沒有什麼。
請幫忙!
我試過了,我仍然在嘗試找回 – 2014-09-10 20:29:06
我在routingconfig中啓用了MapMvcAttributeRoutes,並使用了你的代碼並嘗試點擊它,但仍然是舊的錯誤。有沒有我在web.config中缺少的配置會使其工作? – 2014-09-10 20:58:43