【发布时间】:2014-01-18 15:31:48
【问题描述】:
我正在调用 http://localhost/AppTools.WebAPI/api/BulletinBoard/GetMessagesForApp/AppName,但它返回 404 错误。我认为这与路由有关,但我不确定。
这是我的 BulletinBoard 控制器中的 Web API 方法:
[HttpGet]
public HttpResponseMessage GetMessagesForApp(string id)
{
// get current, valid messages
var messages = (from i in db.BulletinBoards
where i.AppId == id &&
DateTime.Today >= i.DisplayFrom &&
DateTime.Today <= i.DisplayTo &&
i.IsActive == true
select new
{
Message = i.Message,
IntervalId = i.IntervalId,
Interval = i.Interval.IntervalDescription,
Timeout = i.Timout,
})
.ToList();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, messages);
return response;
}
这是我的 RouteConfig.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
标准的Get() 和Get(int id) 工作正常,我没有更改方法名称或签名。 Get() 返回完整的记录列表,Get(int id) 返回特定记录。我希望GetMessagesByApp(string id) 返回特定于某个 AppName 的记录列表。你能说出为什么这不起作用吗?
【问题讨论】:
标签: c# asp.net-mvc asp.net-web-api routes