【问题标题】:MVC 4.5 Web API Routing not working?MVC 4.5 Web API 路由不起作用?
【发布时间】:2013-06-23 12:33:23
【问题描述】:

第一条路线有效。

例如api/Shelves/SpaceTypes/1

第二条路线不起作用。我收到多个操作错误。

例如api/Shelves/1

问)为什么?

这些是我的路线:

config.Routes.MapHttpRoute(
    "DefaultApiWithAction",
    "api/{controller}/{action}/{id}"
);

config.Routes.MapHttpRoute(
    "DefaultApiWithId",
    "api/{controller}/{id}",
    null,
    new { id = @"\d+" }
);

这是我的控制器:

public HttpResponseMessage Get(int id)
{
     ...
}

[ActionName("SpaceTypes")]
public HttpResponseMessage GetSpaceTypes(int id)
{
     ...
}

【问题讨论】:

    标签: asp.net-mvc-4 controller routing asp.net-web-api


    【解决方案1】:

    对于 MVC 4.5,这是唯一有效的方法

    目前有一个bug about this

    为了让您的路由正常工作,请进行以下工作

    api/Shelves/ //Get All Shelves
    api/SpaceTypes/1 //Get Shelf of id 1
    api/Shelves/1/SpaceTypes/  //Get all space types for shelf 1
    

    您需要执行以下操作。

    将您的路由更改为。 (注意默认操作..)

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

    在您的控制器中将基本方法更改为

    [ActionName("DefaultAction")]
    public string Get()
    {
    }
    
    [ActionName("DefaultAction")]
    public string Get(int id)
    {
    }
    
    [ActionName("SpaceTypes")]
    public string GetSpaceTypes(int id)
    {
    }
    

    现在一切都应该按预期工作..

    感谢 Kip Streithorst 完成此,for a full explanation

    【讨论】:

    • 谢谢,我很想有一天见到你,并亲自感谢你
    【解决方案2】:

    我遇到了类似的问题,发现我没有在我的 WebApiConfig 中调用 MapHttpAttributeRoutes 方法...

    希望对你有帮助, 大卫

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

    【讨论】:

      【解决方案3】:

      @Kristof 几乎是对的。您应该选择第二条路线:

      config.Routes.MapHttpRoute(
          "DefaultApiWithId", 
          "api/{controller}/{id}",
          new { action = "Get" },
          new { id = @"\d+ }
          );
      

      【讨论】:

        【解决方案4】:

        此路由不知道要绑定到哪个操作:

        config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", null, new { id = @"\d+" });
        

        您的两种方法都是有效的候选方法。
        我不是 100% 清楚你的设置是什么,但在正常的 REST 中,每个资源都有一个控制器,看起来你有 1 个控制器和 2 个资源。
        要使其在此设置中工作,您可以强制您的第二条路线执行 get 操作,如下所示:

         config.Routes.MapHttpRoute("DefaultApiWithId", "api/{controller}/{id}", null, new { id = @"\d+", action="Get" });
        

        【讨论】:

        • 谢谢,但这不起作用。我在 1 个控制器上没有 2 种资源类型。我有一个单一的资源类型,用另一种方法通过它的 id 获取有关该资源的子信息。
        【解决方案5】:

        确保在项目的 Global.asx 文件中添加了

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        

        进入 Application_Start 函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-14
          • 2018-10-10
          • 2021-08-27
          • 2018-08-29
          • 2014-03-12
          • 1970-01-01
          • 2021-10-02
          • 2017-12-20
          相关资源
          最近更新 更多