c# - 为什么我的 WebAPI RESTful 路由会抛出 404 错误?

标签 c# asp.net-mvc rest asp.net-web-api

在我正在进行的一个副项目中,我正在使用 WebAPI 2.2 创建一个 RESTful API。我正在研究的是一种访问游戏设置的方法。我试图完成的路线示例如下:

http://x/api/GameSettings/             <-- Returns all settings
http://x/api/GameSettings/audio        <-- Returns the 'audio' category
http://x/api/GameSettings/audio/volume <-- Returns the key 'volume' in category audio

注意:示例都是Get请求。

我已经实现了以下 Controller ...

public class GameSettingsController : ApiController
{
    // GET /api/GameSettings
    public HttpResponseMessage Get()
    {
        // Magic
        return Request.CreateResponse(HttpStatusCode.OK, model);
    }

    public HttpResponseMessage Get(string category)
    {
        // Similar.
    }

    public HttpResponseMessage Get(string category, string key)
    {
        // Slightly different, but still similar.
    }
}

我绑定(bind)了以下MVC路由:

// Only necessary for the main view...
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}",
    defaults: new { controller = "Home", action = "Index" }
);

并且,我绑定(bind)了以下 WebAPI 路由:

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
    name: "ApiGeneralCommand",
    routeTemplate: "api/{controller}",
    defaults: new { controller = "GameSettings" }
);

config.Routes.MapHttpRoute(
    name: "ApiCategoryCommands",
    routeTemplate: "api/{controller}/{category})",
    defaults: new { controller = "GameSettings" }
);

config.Routes.MapHttpRoute(
    name: "ApiKeyCommands",
    routeTemplate: "api/{controller}/{category}/{key}",
    defaults: new { controller = "GameSettings", category = "master" },
    constraints: new { key = "[a-z0-9.-]" }
);  

...最后,我的 Global.asax 配置设置如下:

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

...但是有一个小问题。

当我导航到 http://x/api/GameSettings/audio 时,出现 404 错误。就好像请求中的 category 参数未正确关联到我的 Controller 上的 Get(string Category) 方法。这让我相信我的路线是错误的或者我错过了一些东西。

作为健全性检查,我使用非 RESTful 语法 http://x/api/GameSettings?category=audio 测试了该路线,该语法遇到了断点并产生了结果。这只是再次证实了我的理论:WebAPI 路由已关闭。

作为额外的健全性检查,我测试了 http://x/api/GameSettings/,不仅命中了该函数中设置的断点,而且返回了预期结果。

问题:我的路由缺少什么,这将允许 http://x/api/GameSettings/audiohttp://x 一样工作/api/GameSettings?category=audio?我有一段时间没有使用 RESTful API,所以我确信我错过了一些非常愚蠢的东西。

最佳答案

我会尝试使用属性路由。我相信这应该适合您的场景。

[RoutePrefix("api/GameSettings")]
public class GameSettingsController
{
    // GET /api/GameSettings
    [HttpGet]
    public HttpResponseMessage Get()
    {
        // Magic
        return Request.CreateResponse(HttpStatusCode.OK, model);
    }

    [Route("{category}")]
    [HttpGet]
    public HttpResponseMessage Get(string category)
    {
        // Similar.
    }

    [Route("{category}/{key}")]
    [HttpGet]
    public HttpResponseMessage Get(string category, string key)
    {
        // Slightly different, but still similar.
    }
}

我会删除您添加到配置中的内容。

希望这有帮助。

关于c# - 为什么我的 WebAPI RESTful 路由会抛出 404 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777280/

相关文章:

asp.net-mvc - 自动更新网络应用

c# - ASP.NET 在 URL 中传递参数

java - POST 中的 Jersey @HeaderParam null

c# - 检查 StreamReader 是否可以读取另一行

时间:2019-03-17 标签:c#WindowsShutdownprocesshirides

c# - 在附加到新的父 Visual 之前,必须断开指定子项与当前父 Visual 的连接

c# - Entity Framework 代码首先使用导航属性一个Key

javascript - 如何让Javascript同步运行函数

c# - Office 365 REST API

java - 从服务器位置从 REST 返回文件