【问题标题】:Identical route working in RouteConfig, but not AreaRegistration在 RouteConfig 中工作的相同路线,但不是 AreaRegistration
【发布时间】:2014-06-17 00:04:05
【问题描述】:

我在 Web 窗体和 MVC 愉快地生活在一起的网站项目(不是 Web 应用程序)中工作。为了组织我的代码,我尝试使用 MVC 部分设置区域并且遇到了这种情况。

我已经用我的控制器设置了我的区域并创建了以下区域配置:

Namespace Areas.Awesome

    Public Class AwesomeAreaRegistration
        Inherits AreaRegistration

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

        Public Overrides Sub RegisterArea(context As AreaRegistrationContext)

            context.MapRoute(
                "Awesome_default",
                "Awesome/{controller}/{action}/{id}",
                New With {.controller = "Sauce", .action = "Index", .id = UrlParameter.Optional},
                New String() {"Areas.Awesome"}
            )

        End Sub

    End Class

End Namespace

当我尝试导航到 /Awesome/Sauce/ 时,我收到 404 错误,而我的网站实际上试图将我路由到 /Awesome/Sauce/Default.aspx

但是,当我将路线移动到我的RouteConfig:

Public Module RouteConfig

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

        routes.MapRoute(
            "Awesome_default",
            "Awesome/{controller}/{action}/{id}",
            New With {.controller = "Sauce", .action = "Index", .id = UrlParameter.Optional},
            New String() {"Areas.Awesome"}
        )
    End Sub

End Module

这符合预期的/Awesome/Sauce/

我进行了一些挖掘并同时创建了两条路由,但使用不同的 URI,我发现它们都以相同的方式定义,但一个有效,一个无效。

我在区域注册中是否缺少某些东西会导致这些路由被忽略而 RouteConfig 中定义的路由却不是?

【问题讨论】:

    标签: asp.net-mvc webforms routes asp.net-mvc-areas web-site-project


    【解决方案1】:

    可能与命名空间在区域中的应用方式有关。

    this article

    命名空间可能不完全适用于该区域。

    public class ContactsAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Contacts";
            }
        }
    
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Contacts_default",
                "Contacts/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MvcApplication1.Areas.Contacts.Controllers" }
            );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2018-05-15
      • 2016-11-16
      相关资源
      最近更新 更多