c# - 如何为路由注册区域

标签 c# asp.net asp.net-mvc asp.net-mvc-4

我创建了具有 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

我该如何解决这个问题? 谢谢。

最佳答案

RegisterRoutes 的某处调用 AreaRegistration.RegisterAllAreas()

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

提示:使用类似 RouteDebugger 2.0 的工具或 Routing Debugger调查你的路线

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

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

关于c# - 如何为路由注册区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13454699/

相关文章:

c# - uri 的相对路径

c# - 为什么 Visual Studio 有时不转到我的断点?

asp.net-mvc - ASP.NET MVC-Elmah无法正常工作,并为elmah.axd返回404页

asp.net-mvc - ASP.Net MVC中的文件上传遇到之前的错误时Request.File.Count仍然设置为1

c# - 处理合并的业务和表示代码的最佳方式?

c# - 任务继续未安排在线程池线程上

c# - 如何使用 F# lambda 创建 Linq 表达式树?

ASP.NET 多个联合身份提供商

asp.net - 将确认或错误消息传递回 View 的好方法是什么?

javascript - 通过 javaScript 或 jQuery 在鼠标单击时读取表格单元格值