我创建了具有3个不同区域的MVC应用程序。 (管理员,用户,新闻)
这是我在App_Start目录中的RouteConfig.cs文件:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "TestMvcApplication.Controllers" }
        );
    }
}

这是我的AdminAreaRegisteration.cs文件:
    namespace TestMvcApplication.Areas.Admin
{
    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 },
                namespaces: new[] { "TestMvcApplication.Areas.Admin.Controllers" }
            );
        }
    }
}

最后这是我的Global.asax.cs文件内容:
namespace TestMvcApplication
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode,
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }
}

我的网站主页已满载,并且可以正常工作。但是管理员主页或其他区域无法按路线检测,我给出了此错误消息:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Admin/Home

我怎么解决这个问题?
谢谢。

最佳答案

AreaRegistration.RegisterAllAreas()中的某处调用RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    AreaRegistration.RegisterAllAreas();
    ....
}

提示:使用RouteDebugger 2.0Routing Debugger之类的工具调查您的路线

获取最新的NuGet:
Route Debugger for MVCRouteDebugger for WepApi

这是有关How to set up and use RouteDebugger with WebApi的教程

10-08 03:16