MVC Attribute Routing Not Working

前端 未结 5 1793
孤独总比滥情好
孤独总比滥情好 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条回答
  •  爱一瞬间的悲伤
    2020-12-01 14:08

    Not only do you have to have the call to routes.MapMvcAttributeRoutes() in your App_Start\RouteConfig.cs file, it must appear before defining your default route! I add it before that and after ignoring {resource}.axd{*pathInfo}. Just found that out trying to get attribute routing to work correctly with my MVC 5 website.

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapMvcAttributeRoutes();
    
            routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
            );
    
        }
    

提交回复
热议问题