属性路由不工作MVC5

时间:2015-10-08 20:32:04

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

这是一篇很长的帖子。我正在使用如下所述的属性路由: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx#enabling-attribute-routing

我已放入WebApiConfig.cs:

public static void Register(HttpConfiguration config)
         {
             config.EnableCors();
+            config.MapHttpAttributeRoutes();
             config.Routes.MapHttpRoute(
                 name: "DefaultApi",
                 routeTemplate: "api/{controller}/{id}",
}

在Global.asax.cs

             AreaRegistration.RegisterAllAreas();
+            //WebApiConfig.Register(GlobalConfiguration.Configuration);
+            GlobalConfiguration.Configure(WebApiConfig.Register);
             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
             RouteConfig.RegisterRoutes(RouteTable.Routes);

我正在使用webapi控制器:

public class HelloController : ApiController
    {
[Route("Services/test/Application/{id}")]
        public string GetTest(int id)
        {
    return "1";
    }
}

我正在使用Postman Chrome扩展程序进行测试。在我自己的计算机上,当我在Visual Studio中测试时,这是完美的工作:http://localhost:6296/Services/test/Application/12 并返回预期的结果,但在我在站点上部署后,它不起作用:http://www.mytest.com/Services/test/Application/12(甚至在服务器localhost:http://localhost/Services/test/Application/12上测试) 并返回:

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 
Requested URL: /Services/test/Application/12

参考System.Web.Mvc(版本5.2.3.0)标记为“copy local = true”。没有使用授权。经典的webapi控件可以在服务器和本地完美运行。

问题:可能出现什么问题,我应该从哪里开始寻找?!

1 个答案:

答案 0 :(得分:-1)

将此添加到您的WebApiConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{

    routes.MapMvcAttributeRoutes();
}