【问题标题】:Trying to call a specific GET method on my WebAPI, but getting HTTP 404 Not Found尝试在我的 WebAPI 上调用特定的 GET 方法,但得到 HTTP 404 Not Found
【发布时间】: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


    【解决方案1】:

    这是我的 RouteConfig.cs:

    RouteConfig.cs 文件用于定义 ASP.NET MVC 控制器的路由。这些与您的 Web API 控制器使用的路由完全无关。它们在WebApiConfig.cs 文件中定义。

    所以请确保您已在正确的位置声明您的路线:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "ApiWithActionName",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
    

    请注意,我在默认路由之前添加了一个自定义路由,这将允许您实现所需的 url 模式。

    然后您可以使用以下控制器操作,它可以正常工作:

    // GET /api/controllername
    // GET /api/controllername/get
    [HttpGet]
    public HttpResponseMessage Get()
    {
        ...
    }
    
    // GET /api/controllername/get/123
    [HttpGet]
    public HttpResponseMessage Get(int id)
    {
        ...
    }
    
    // GET /api/controllername/GetMessagesForApp/abc
    [HttpGet]
    public HttpResponseMessage GetMessagesForApp(string id)
    {
        ...
    }
    

    【讨论】:

    • 谢谢,但出于好奇,为什么 RouteConfig.cs 会包含在 WebAPI 项目中?
    • 因为您创建的不是 Web API 项目,而是可用于托管 Web API 的 ASP.NET MVC 项目模板。您可以创建一个空的 ASP.NET 应用程序(推荐),然后设置您只需要的东西。
    • RouteConfig.cs 专门用于配置 ASP.NET 路由,而 WebApiConfig.cs 可用于任何与 Web API 相关的配置,包括特定于 Web API 的路由、Web API 服务和任何其他 Web API设置。
    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2019-01-13
    • 2021-04-20
    相关资源
    最近更新 更多