-->

MVC5 Area not behaving properly

2019-07-29 13:04发布

问题:

This post is directly related to: MVC5 Area not working

That post fixed the index.cshtml issue, however it did not resolve each view for that controller. For example:

http://localhost:45970/Setup/Start gives the error that the resource cannot be found (basically a 404).

However http://localhost:45970/Setup/Setup/Start brings up the correct page.

So what needs to be reconfigured so that ALL views for that controller in the Setup Area will open properly?

Edit 1

Code from SetupAreaRegistration.cs

using System.Web.Mvc;

namespace BlocqueStore_Web.Areas.Setup
{
    public class SetupAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Setup";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                name: "Setup_default",
                url: "Setup/{controller}/{action}/{id}",
                defaults: new { action = "Index", controller = "Setup", id = UrlParameter.Optional },
                namespaces: new[] { "BlocqueStore_Web.Areas.Setup.Controllers" }
            );
        }
    }
}

回答1:

Since you have an Area named Setup with the above route configuration, opening http://localhost:45970/Setup/Start will execute StartController under Setup Area. You got 404 error because you don't have StartController under Setup Area, but you can open http://localhost:45970/Setup/Setup/Start successfully because you have SetupController and Start action method under Setup Area.

Based on your comment, you want the following url patterns

http://{host}/Setup/‌​{view}
http://{host}/Admin/‌​{view}

You can accomplish that without using any Area. You only need AdminController and SetupController using the default route.