如何注册路由区 [英] How to register areas for routing

查看:141
本文介绍了如何注册路由区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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文件:

And This is my AdminAreaRegisteration.cs file:

    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文件内容:

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

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.

推荐答案

呼叫AreaRegistration.RegisterAllAreas()在你的RegisterRoutes某处

Call AreaRegistration.RegisterAllAreas() somewhere in your RegisterRoutes

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

提示::使用一个像 RouteDebugger 2.0 工具或路由调试来调查你的路由

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

获取最新的NuGet:
路线调试器MVC 或的 RouteDebugger为WepApi

Get latest NuGet: Route Debugger for MVC or RouteDebugger for WepApi

继承人在<一个教程href=\"http://blogs.msdn.com/b/webdev/archive/2013/04/04/debugging-asp-net-web-api-with-route-debugger.aspx\">How设置和使用RoteDebugger用的WebAPI

这篇关于如何注册路由区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆