C# 如何注册路由区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13454699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:37:17  来源:igfitidea点击:

How to register areas for routing

c#asp.netasp.net-mvcasp.net-mvc-4

提问by Mojtaba

I created MVC Application that have 3 different Area. (Admin, User, News) This is my RouteConfig.cs File in App_Start directory:

我创建了具有 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" }
        );
    }
}

And This is my AdminAreaRegisteration.cs file:

这是我的 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" }                
            );
        }
    }
}

And finally this is my Global.asax.cs file content:

最后这是我的 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();
        }
    }
}

Home page of my website fully loaded and it's works. but Homepage of Admin or other areas are not detect by route and I gave this error message:

我网站的主页已完全加载并且可以正常工作。但是没有通过路由检测到管理员主页或其他区域,我给出了以下错误消息:

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

How can I solve this problem? Thanks.

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

采纳答案by Lukas Winzenried

Call AreaRegistration.RegisterAllAreas()somewhere in your RegisterRoutes

AreaRegistration.RegisterAllAreas()在你的某个地方打电话RegisterRoutes

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

Tip:Use a tool like RouteDebugger 2.0or Routing Debuggerto investigate your routes

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

Get latest NuGet: Route Debugger for MVCor RouteDebugger for WepApi

获取最新的 NuGet: MVC 的 Route DebuggerWepApi 的 RouteDebugger

Here's a tutorial on How to set up and use RouteDebugger with WebApi

这是关于如何使用 WebApi 设置和使用 RouteDebugger 的教程

回答by James

From the code provided I can see 2 potential issues:

从提供的代码中,我可以看到 2 个潜在问题:

  1. You aren't calling RegisterAllAreas
  2. You don't appear to be overriding the AreaNameproperty
  1. 你不是在调用RegisterAllAreas
  2. 您似乎没有覆盖AreaName属性

Try changing your code to:

尝试将您的代码更改为:

Global.asax

全球.asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    AreaRegistration.RegisterAllAreas();
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "TestMvcApplication.Controllers" }
    );
}

Admin area

行政区域

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

回答by Dice

Just create a static class name it AreaConfigwith a static method RegisterAreas()here code:

只需使用静态方法RegisterAreas()在这里代码创建一个静态类名称它AreaConfig

public static class AreaConfig
{
    public static void RegisterAreas()
    {
        // 
        // Admin area . . .

        var adminArea = new AdminAreaRegistration();
        var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes);
        adminArea.RegisterArea(adminAreaContext);


        // 
        // Default area . . .

        var defaultArea = new DefaultAreaRegistration();
        var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes);
        defaultArea.RegisterArea(defaultAreaContext);
    }
}

then call it in a Global.asax.cs file like this:

然后在 Global.asax.cs 文件中调用它,如下所示:

protected void Application_Start()
    {
        . . .

        AreaConfig.RegisterAreas();

        . . .
    }