S
S
skosterin882017-01-06 19:32:38
ASP.NET
skosterin88, 2017-01-06 19:32:38

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

            
        }
    }

Here is the contents of the RouteConfig.cs file:
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" }
            );
        }
    }

And this is my Global.asax.cs:
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);
        }
    }

I would like to make it so that when the application starts, the Index method of the HomeController controller from the Default area is launched by default. Those. so that the URLs localhost:12345 and localhost:12345/Default/Home/Index are equivalent.
The problem is this - under the conditions above, when I just take and run the project, the browser gives me the error "The web server is configured in such a way as not to generate a list of the contents of the directory" with the comment "This error occurs when the document is not specified in URL address, the default document for the website or app is not specified, and when the tools to display a catalog listing are not enabled for the website or app. This setting may be disabled intentionally to protect server content."
Questions:
1) Is it possible to implement what was planned using ASP .NET MVC 5?
2) If yes, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
skosterin88, 2017-01-06
@skosterin88

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

            
        }

After these changes, everything worked for me as it was intended.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question