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

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

问题描述

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

推荐答案

Call AreaRegistration.RegisterAllAreas() 在你的 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 路由调试器WepApi 的路由调试器

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

这是关于 如何通过 WebApi 设置和使用 RouteDebugger

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

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