RouteCollection' doesn't contain a definition for 'MapMvcAttributeRoutes

久未见 提交于 2019-12-20 01:40:15

问题


I just had to downgrade my ASP.Net 4.5.2 application to ASP.Net 4.0. Of course this brings problems with it, like references that are not installed correct. I solved some of them already, but I can't get my head around an error:

CS106 'RouteCollection' does not contain a definition for 'MapMvcAttributeRoutes' and no extension method 'MapMvcAttributeRoutes' accepting a first argument of type 'RouteCollection' could be found

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "device", action = "view", id = UrlParameter.Optional });
        }
    }

Does someone know what I have to do here?

Extra info

Namespaces that I use:

  • using System.Web
  • using System.Web.Mvc
  • using System.Web.Routing
  • using System.Web.Http

Visual studio Community 2015


回答1:


The only version of MVC that supports attribute routing (which provides support for the MapMvcAttributeRoutes extension method) is MVC 5.

However, MVC 5 only supports .NET framework 4.5 and higher.

So, you have 2 options:

  1. Stay on .NET Framework 4.5+
  2. Downgrade to MVC 4 and either:
    1. Ditch attribute routing altogether and use convention-based routing
    2. Go with the open source attribute routing that supported MVC 3 and 4

Being that Microsoft officially no longer supports any version of .NET Framework lower than 4.5.2 (except for 3.5, but that would mean downgrading to MVC 2 for support), I would highly recommend you consider the first option seriously.




回答2:


I used routes.Ignore(). It seems to be working.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes) {
        routes.Ignore("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "device", action = "view", id = UrlParameter.Optional });
    }
}


来源:https://stackoverflow.com/questions/37991768/routecollection-doesnt-contain-a-definition-for-mapmvcattributeroutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!