我创建了MVC应用程序,它有三个不同的区域.(管理员、用户、新闻)

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

这是我的行政登记.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

我怎样才能解决这个问题?

推荐答案

RegisterRoutes的某个地方打AreaRegistration.RegisterAllAreas()

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

Tip:使用RouteDebugger 2.0Routing Debugger等工具调查路由

获取最新NuGet:

这里有一个关于How to set up and use RouteDebugger with WebApi的教程

Asp.net相关问答推荐

DBSet 不包含 Where 的定义

如何正确配置 IHttpModule?

在 EF4.1 中正确地从上下文中附加和分离实体

asp.net dropdownlist - 在 db 值之前添加空行

ASP.NET GridView 第二个标题行跨越主标题行

如何使用文件上传控件 Select 多个文件?

如何在 ASP.NET Web API 中使用非线程安全的 async/await API 和模式?

Jquery asp.net 按钮单击事件通过 ajax

如何在 ASP.NET MVC3 中配置区域

对于每个请求,RestClient 应该是单例还是新的

IIS 会话超时与 ASP.NET 会话超时

asp.net 有控制台日志(log)吗?

Fiddler 没有从 ASP.NET 网站嗅探 SOAP 流量

IIS 8.0 ASP.NET 和错误 500.19

如何将列表转换为数据表

ASP.NET:在 Response.Redirect(...) 之后代码会发生什么?

盒式磁带包与 MVC4 包

使用 ConfigurationManager.RefreshSection 重新加载配置而不重新启动应用程序

连接字符串正确时出现实例失败错误

如何在不遍历每个循环的情况下从字典对象中获取所有键(仅键)