asp.net-mvc - VB 中的 ASP NET MVC 区域路由/多路由问题

标签 asp.net-mvc vb.net routes areas asp.net-mvc-3-areas

我对 .net 非常缺乏经验,刚刚开始学习 MVC。我遇到了一个关于发现多个 Controller 的问题:

“发现多个类型与名为‘reviews’的 Controller 相匹配。如果为此请求提供服务的路由 (‘{controller}/{action}/{id}’) 没有指定命名空间来搜索匹配的 Controller ,就会发生这种情况请求。如果是这种情况,请通过调用带有 'namespaces' 参数的 'MapRoute' 方法的重载来注册此路由。

我最近在我的应用程序中添加了一个新的“管理”区域,其中有一个“ReviewController”。主应用程序文件夹中还有一个“ReviewController”:

啊 - 作为新用户,我无法发布图像,但基本上我在“ Controller ”和“区域/管理/ Controller ”中有一个“ReviewController”。

到目前为止,我已经设置了 2 条路线:

Global.asax.vb 中的默认路由

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

     ' MapRoute takes the following parameters, in order: 
     ' (1) Route name
     ' (2) URL with parameters
     ' (3) Parameter defaults

     routes.MapRoute( _
       "Default", _
       "{controller}/{action}/{id}", _
       New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, _
       {"PowellCasting/Controllers"}
     )

  End Sub

  Sub Application_Start()

    AreaRegistration.RegisterAllAreas()

    System.Data.Entity.Database.SetInitializer(New System.Data.Entity.DropCreateDatabaseIfModelChanges(Of Models.PowellCastingEntites))
    Database.SetInitializer(Of PowellCastingEntites)(New PowellCastingInitializer())

    RegisterGlobalFilters(GlobalFilters.Filters)
    RegisterRoutes(RouteTable.Routes)

    ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers")

  End Sub

AdminAreaRegistration 中的区域路由
Namespace PowellCasting.Areas.Admin
  Public Class AdminAreaRegistration
    Inherits AreaRegistration

    Public Overrides ReadOnly Property AreaName() As String
      Get
        Return "Admin"
      End Get
    End Property

    Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
      context.MapRoute( _
        "Admin_default", _
        "Admin/{controller}/{action}/{id}", _
         New With {.Controller = "Dashboard", .action = "Index", .id = UrlParameter.Optional}
       )
    End Sub
  End Class
End Namespace

在阅读了我遇到的问题后,我添加了一些代码:

我的管理 Controller 定义了正确的命名空间
  • 命名空间 PowellCasting.Areas.Admin 而不是简单的 PowellCasting。
  • 我在全局设置了 RegisterAllAreas
  • ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers") 用于指定默认路由。

  • 我现在遇到的具体问题是,当我转到“/评论”时,我收到了上面显示的多个 Controller 错误,具体来说:

    *“评论”请求找到了以下匹配的 Controller :
    PowellCasting.PowellCasting.Areas.Admin.ReviewsController

    PowellCasting.PowellCasting.ReviewsController*

    我启用了路由调试器,只显示一个匹配项:

    啊 - 作为新用户,我无法发布图片,但它显示:

    Admin/{controller}/{action}/{id} 为 FALSE



    {controller}/{action}/{id} 为 TRUE

    这是预期的,所以我不知道为什么我会收到这个问题。

    我已经阅读了有关使用命名空间重载 maproute 方法的内容,但在 VB 中找不到示例(在 c# 中加载)。但我试过这个:
    Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
      context.MapRoute( _
          "Admin_default", _
         "Admin/{controller}/{action}/{id}", _
          New With {.Controller = "Dashboard", .action = "Index", .id = UrlParameter.Optional}, _
          vbNull,
          {"PowellCasting/Areas/Admin/Controllers"}
      )
    End Sub
    


    Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
    
    ' MapRoute takes the following parameters, in order: 
    ' (1) Route name
    ' (2) URL with parameters
    ' (3) Parameter defaults
    
    routes.MapRoute( _
        "Default", _
        "{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, _
        vbNull,
        {"PowellCasting/Controllers"}
    )
    
    End Sub
    

    但没有成功。

    我相信这应该很简单,而且我已经尝试了很多方法——这非常令人沮丧。任何帮助将非常感激。

    我在这里的第一篇文章 - 嗨! :)

    最佳答案

    如果您仔细阅读您收到的错误消息:

    The request for 'reviews' has found the following matching controllers: PowellCasting.PowellCasting.Areas.Admin.ReviewsController PowellCasting.PowellCasting.ReviewsController



    你会注意到 PowellCasting重复两次。

    因此,在您的主要 Global.asax 路线注册中:
    routes.MapRoute( _
        "Default", _
        "{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}, _
        vbNull,
        {"PowellCasting.Controllers"}
    )
    

    这假设您的 ReviewController在根中定义如下:
    Namespace Controllers
        Public Class ReviewController
            Inherits System.Web.Mvc.Controller
    
            Function Index() As ActionResult
                Return View()
            End Function
        End Class
    End Namespace
    

    注意 PowellCasting 的缺失命名空间定义中的前缀(这是一个自动添加它的 VB subtility,我想这是您的应用程序的名称)

    和内部 AdminAreaRegistration.vb :
    context.MapRoute( _
        "Admin_default", _
        "Admin/{controller}/{action}/{id}", _
        New With {.action = "Index", .id = UrlParameter.Optional}, _
        vbNull, _
        {"PowellCasting.Areas.Admin.Controllers"}
    )
    

    假设您的 ReviewController在这个领域是这样定义的
    Namespace Areas.Admin.Controllers
        Public Class ReviewController
            Inherits System.Web.Mvc.Controller
    
            Function Index() As ActionResult
                Return View()
            End Function
        End Class
    End Namespace
    

    再次注意到 PowellCasting 的缺失命名空间定义中的前缀。

    关于asp.net-mvc - VB 中的 ASP NET MVC 区域路由/多路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7423962/

    相关文章:

    c# - 扩展方法中的 HtmlAttributes

    ruby-on-rails - 如何在 Rails 4 中将一组资源路由别名或重写到另一个命名空间?

    vb.net - 未定义用户控件类型

    c# - 将 C# 代码转换为 VB.NET

    javascript - 在 React/Next js 中路由之前检查更改

    javascript - Express 路由器的功能 app.use(path, require path) 是如何工作的?

    javascript - 无法使用 Tab 键选择 Kendo Multiselect 的数据

    c# - 如何在 VB.NET 或 C# 中将此 WindsorControllerFactory 转换为 UnityControllerFactory

    c# - 使 Controller 操作的 "return partialview()"自动链接到 asp.net mvc3 中的 "_partialview.cshtml"

    jquery - 从代码隐藏执行与类关联的 jQuery 对话框