I would like to avoid the call
AreaRegistration.RegisterAllAreas ()
in my global.asax because I'm trying to move all the startup logic into separate classes inside the App_Start folder. However, I was not successful at work. The first option tried to use this code:
[assembly: PreApplicationStartMethod(typeof(Startup), "PreInit")] namespace Foo { public class Startup {} }
Where PreApplicationStartMethod comes from the System.Web namespace. In this case, the call to the registers is too early.
A second approach, based on this post by David Abbo , uses WebActivator: using System.Web.Mvc;
[assembly: WebActivatorEx.PostApplicationStartMethod (typeof(AreaDemo.AreaConfig), "RegisterAreas")] namespace AreaDemo { public class AreaConfig { public static void RegisterAreas() { AreaRegistration.RegisterAllAreas(); } } }
Unfortunately, despite the fact that no error was selected, an attempt to move to the area is not performed (as if registration had never occurred).
What is the correct way to register areas in ASP.NET MVC 5 from a startup class using the build directive rather than directly calling from Global.asax?
Update 1: Here is my registration area code:
public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new string[] { "AreaDemo.Areas.Admin.Controllers" } ); } }
For some reason, the default values ββare ignored, but the transition to / admin / home / index / 0./admin,/admin/home and / admin / home / index all 404 works.