路由不按预期工作

时间:2014-09-11 18:25:03

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

我希望有一个WebAPI控制器" ContractController"需要获取和使用uri的帖子,如

    (POST)
    http://locolhost:34567/api/v1/Contract

    (GET)
    http://locolhost:34567/api/v1/Contract/1234567890

我的路线几乎按我需要的方式工作:

  config.Routes.MapHttpRoute(
          "GetPost",
          "api/v1/Policy/{id}",
          new { controller = "Policy", id = RouteParameter.Optional });

除非我在Fiddler中执行帖子:http://locolhost:34567/api/v1/Contract 我得到了404.

如果我在“合同”之后添加任何内容

http://locolhost:34567/api/v1/Contract/Foo
http://locolhost:34567/api/v1/Contract/12



 It works as expected but 

http://locolhost:34567/api/v1/Contract
or
http://locolhost:34567/api/v1/Contract/ 

give a HTTP/1.1 404 Not Found.

The controller has only 2 public methods (GetContract & PostContract)
Any thoughts on what I am missing?

1 个答案:

答案 0 :(得分:0)

好吧,我终于找到了问题所在。

我的GLobal.asa文件

        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        GlobalConfiguration.Configure(WebApiConfig.Register);

答案是在路线注册时反转顺序。

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

这个项目也有帮助 http://blogs.msdn.com/b/webdev/archive/2013/04/04/debugging-asp-net-web-api-with-route-debugger.aspx