簡體   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