MVC Attribute Routing Not Working

前端 未结 5 1769
孤独总比滥情好
孤独总比滥情好 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:04

    I came here looking for answers related to RoutePrefix. After some testing, I found that simply adding a

    [RoutePrefix("MyPrefix")]

    without using a subsequent Route attribute such as

    [Route("MyRoute")]

    means the RoutePrefix was ignored. Haack's routedebugger is very helpful in determining this: https://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/

    Simply add it via NuGet, which will add a line to your appsettings, and then all your routes are displayed at the bottom of your page. Highly recommend for any routing issues.

    In the end, my final version looks like:

    [RoutePrefix("Asset/AssetType")] [Route("{action=index}/{id?}")]

    0 讨论(0)
  • 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 }
            );
    
        }
    
    0 讨论(0)
  • 2020-12-01 14:13

    Ensure you have NuGet package "WebApp API" installed for AttributeRouting.

    0 讨论(0)
  • 2020-12-01 14:15

    In your App_Start/RoutesConfig.cs

    make sure you call the following line of code:

      routes.MapMvcAttributeRoutes();
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题