Answer the question
In order to leave comments, you need to log in
How to run a site on ASP .NET MVC from a specific area (area)?
Hello gentlemen and ladies (if any).
There is a project on ASP .NET MVC 5. It is divided into two areas - Admin and Default. The idea is this - everything intended for mere mortals is in the Default area, the admin panel is, accordingly, the Admin area.
This is how routes are registered in the Default area (DefaultAreaRegistration.cs):
public class DefaultAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Default";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Default",
"Default/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "InstrumentsStore.Areas.Default.Controllers" }
);
context.MapRoute(
"Catalog",
"Default/{controller}/{action}/{id}",
new { controller = "Catalog", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "InstrumentsStore.Areas.Default.Controllers" }
);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "_Default",
url: "Default/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "InstrumentsStore.Areas.Default.Controllers" }
);
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var adminArea = new AdminAreaRegistration();
var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes);
adminArea.RegisterArea(adminAreaContext);
var defaultArea = new DefaultAreaRegistration();
var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes);
defaultArea.RegisterArea(defaultAreaContext);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
Answer the question
In order to leave comments, you need to log in
I resolved the issue.
For those who are interested, this is how it is done.
1) Remove all route registrations in the RouteConfig.cs file.
2) In the DefaultAreaRegistration.cs file, in the Default area, remove the word "Default" from the URL templates when registering routes. It should turn out like this:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "InstrumentsStore.Areas.Default.Controllers" }
);
context.MapRoute(
"Catalog",
"{controller}/{action}/{id}",
new { controller = "Catalog", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "InstrumentsStore.Areas.Default.Controllers" }
);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question