Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did the order of Areas in RegisterAllAreas change with Visual Studio 2015?

After installing Visual Studio 2015 on multiple machines, the order of routes in the routemap picked up by AreaRegistration.RegisterAllAreas() seems to have reversed itself.

I have 3 areas in my application, each of which has its own AreaRegistration implementation and registers its own specific route.

On machines with only Visual Studio 2013, the list of routes (as reported by the RouteDebugger plugin) was:

SomeArea/{controller}/{action}/{id}
DifferentArea/{controller}/{action}/{id}
{controller}/{action}/{id}

On machines with Visual Studio 2015 (and the exact same source code)

{controller}/{action}/{id}
SomeArea/{controller}/{action}/{id}
DifferentArea/{controller}/{action}/{id}

This was causing 404 errors because the more general route was now matching routes that it did not previously. Why would the order in which the areas were scanned for their AreaRegistration classes change with the installation of a new version of Visual Studio?

like image 481
Mathew Avatar asked Aug 19 '15 07:08

Mathew


2 Answers

Per the solution listed here: http://forums.asp.net/t/1642939.aspx

I took it and did it one better...saves you from having to explicitly list all classes for the generic invocation:

    public static void RegisterArea(Type t, RouteCollection routes, object state)
    {
        AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(t);
        AreaRegistrationContext context = new AreaRegistrationContext(registration.AreaName, routes, state);
        string tNamespace = registration.GetType().Namespace;
        if ( tNamespace != null )
            context.Namespaces.Add(tNamespace + ".*");
        registration.RegisterArea(context);
    }

    protected void Application_Start()
    {
        //AreaRegistration.RegisterAllAreas();

        Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(AreaRegistration)))
            .OrderBy(r=> r.FullName)
            .ToList()
            .ForEach(r=> RegisterArea(r, RouteTable.Routes, null));

        ....

Hope this saves someone some time.

like image 56
Jesse MacNett Avatar answered Oct 15 '22 20:10

Jesse MacNett


Morning,

Same thing here. I installed VS 2015, opened the solution, built and something had happened to Routing.

I then installed RouteDebugger and could see that, like you, Area routing order had changed.

I searched a bit and found this:

http://www.c-sharpcorner.com/UploadFile/c60c74/net-mvc-area-registration-sequence/

Apparently the order of the Compile elements inside the CSPROJ file is the order in which files are sent to compiler and hence, the order in which Areas routes will be registered.

Unfortunately, I think this was valid up until VS 2013. I even took the compiler (CSC.EXE) call from both Output windows and compared all arguments one by one. Nothing different there so my guess is that the compiler itself has somehow changed.

I will raise this with Microsoft but until then I found this easy way of manually registering the Areas:

http://forums.asp.net/t/1642939.aspx

Hope it helps.

Miquel

like image 35
Miquel Parejo Avatar answered Oct 15 '22 20:10

Miquel Parejo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!