You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVC Areas路由配置疑问:默认路由匹配逻辑不符合预期

解决ASP.NET MVC Area路由匹配问题

核心问题分析

你的路由配置存在两个关键问题:

  • 两个Area路由的URL模式完全相同({controller}/{action}/{id}),未给Area添加专属前缀,导致路由匹配按顺序优先命中第一个路由,后续默认路由永远不会被触发。
  • 直接在RouteConfig中绑定Area的方式不规范,未利用MVC内置的AreaRegistration机制,导致URL生成逻辑混乱。

解决方案

1. 修正RouteConfig路由配置

为Area路由添加明确的区域前缀,保留无前缀的默认路由指向General区域:

Public Module RouteConfig
    Public Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
        routes.MapMvcAttributeRoutes()

        ' 带Secure前缀的路由,仅匹配/Secure开头的请求
        routes.MapRoute(
            name:="Secure",
            url:="Secure/{controller}/{action}/{id}",
            defaults:=New With {.controller = "Manage", .action = "Landing", .id = UrlParameter.Optional},
            namespaces:=New String() {"YourProjectName.Areas.Secure.Controllers"}
        ).DataTokens = New RouteValueDictionary(New With {.area = "Secure"})

        ' 无前缀的默认路由,匹配根路径及无区域前缀的请求,指向General区域
        routes.MapRoute(
            name:="Default",
            url:="{controller}/{action}/{id}",
            defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional},
            namespaces:=New String() {"YourProjectName.Areas.General.Controllers"}
        ).DataTokens = New RouteValueDictionary(New With {.area = "General"})
    End Sub
End Module

注意:替换YourProjectName为你的实际项目名称,添加namespaces参数可避免不同Area中同名控制器的冲突。

2. 确保AreaRegistration类正确配置

创建Area时MVC会自动生成对应AreaRegistration类,确保配置如下:

  • SecureAreaRegistration.vb
Public Class SecureAreaRegistration
    Inherits AreaRegistration

    Public Overrides ReadOnly Property AreaName As String
        Get
            Return "Secure"
        End Get
    End Property

    Public Overrides Sub RegisterArea(ByVal context As AreaRegistrationContext)
        context.MapRoute(
            "Secure_default",
            "Secure/{controller}/{action}/{id}",
            New With {.action = "Index", .id = UrlParameter.Optional},
            namespaces:=New String() {"YourProjectName.Areas.Secure.Controllers"}
        )
    End Sub
End Class
  • GeneralAreaRegistration.vb
Public Class GeneralAreaRegistration
    Inherits AreaRegistration

    Public Overrides ReadOnly Property AreaName As String
        Get
            Return "General"
        End Get
    End Property

    Public Overrides Sub RegisterArea(ByVal context As AreaRegistrationContext)
        context.MapRoute(
            "General_default",
            "{controller}/{action}/{id}", ' 无前缀配置,让General区域处理无前缀请求
            New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional},
            namespaces:=New String() {"YourProjectName.Areas.General.Controllers"}
        )
    End Sub
End Class

3. 在Global.asax中注册所有Area

确保Application_Start方法中先注册Area,再注册全局路由:

Sub Application_Start()
    AreaRegistration.RegisterAllAreas()
    RouteConfig.RegisterRoutes(RouteTable.Routes)
    ' 其他配置(如FilterConfig、BundleConfig)
End Sub

效果验证

  • 根路径请求(/)自动匹配General区域的Home/Index,无需添加General前缀。
  • 访问Secure区域页面可通过/Secure/Manage/Landing直接访问,或通过/Secure触发路由默认值。
  • 无前缀的控制器请求(如/Home/About)自动映射到General区域的对应控制器。

内容的提问来源于stack exchange,提问作者gchq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:23:11