MVC Attribute Routing Not Working

前端 未结 5 1790
孤独总比滥情好
孤独总比滥情好 2020-12-01 13:30

I\'m relatively new to the MVC framework but I do have a functioning Web Project with an API controller that utilizes AttributeRouting (NuGet package) - however, I\'m starti

5条回答
  •  Happy的楠姐
    2020-12-01 14:28

    In nuGet package manager install to your project Web API Web Host package

    add this class to folder app_start-> WebApiConfig.cs(if it doesn't exits - create):

      public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.MapHttpAttributeRoutes(); // pay attention to this method
    //here you can map any mvc route
                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //);
                config.EnableSystemDiagnosticsTracing();
            }
        }
    

    after Try change your Global.asax configuration to:

    public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                GlobalConfiguration.Configure(WebApiConfig.Register);
            }
        }
    

    P.S.

    look through this article, very useful http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    cheers

提交回复
热议问题