繁体   English   中英

属性路由不起作用MVC5

[英]Attribute routing not working MVC5

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

我放置在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)被标记为“复制本地= true”。 不使用授权。 经典的webapi控件在服务器和本地均可完美运行。

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

将此添加到您的WebApiConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{

    routes.MapMvcAttributeRoutes();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM