MVC Attribute Routing Not Working

孤街醉人 提交于 2019-11-27 01:57:01

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 }
        );

    }

In your App_Start/RoutesConfig.cs

make sure you call the following line of code:

  routes.MapMvcAttributeRoutes();

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

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?}")]

4rchie

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

I faced this issue today, found that if I had to use Route attribute on an action, I must add RoutePrefix attribute on controller also.

Controller =>

    [RoutePrefix("api/search")]
    public class SearchController : ApiController

Action =>

    [HttpGet]
    [Route("suburbs")]
    public IEnumerable<Model.Suburb> Suburbs([FromUri]string query = "")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!