ASP NET MVC区域路由/ VB中的多个路由问题

Ian*_*Ian 4 vb.net asp.net-mvc routes areas asp.net-mvc-3-areas

我对.net很缺乏经验,刚开始学习MVC.我遇到了一个有关找到多个控制器的问题:

"找到了与名为'reviews'的控制器匹配的多个类型.如果为此请求提供服务的路由('{controller}/{action}/{id}')未指定名称空间来搜索匹配的控制器,则会发生这种情况.如果是这种情况,请通过调用带有'namespaces'参数的'MapRoute'方法的重载来注册此路由."

我最近在我的应用程序中添加了一个新的"Admin"区域,其中我有一个"ReviewController".主app文件夹中还有一个"ReviewController":

啊 - 作为一个新用户,我无法发布图像,但基本上我在"控制器"和"区域/管理员/控制器"中有一个"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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

我的Admin控制器定义了正确的命名空间

  • 命名空间PowellCasting.Areas.Admin而不仅仅是PowellCasting.
  • 我在全球范围内设置了RegisterAllAreas
  • ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers")用于指定默认路由.

我现在遇到的具体问题是,当我进入"/评论"时,我得到上面显示的多个控制器错误,具体来说:

*"评论"请求找到了以下匹配的控制器: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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

但没有成功.

我确信这应该是直截了当的,我尝试了很多东西 - 非常令人沮丧.任何帮助将非常感激.

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

Dar*_*rov 5

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

对"评论"的请求找到了以下匹配的控制器: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"}
)
Run Code Online (Sandbox Code Playgroud)

这假设您ReviewController在根中的定义如下:

Namespace Controllers
    Public Class ReviewController
        Inherits System.Web.Mvc.Controller

        Function Index() As ActionResult
            Return View()
        End Function
    End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)

注意PowellCasting命名空间定义中没有前缀(这是一个自动添加它的VB子操作,我想这是你的应用程序的名称)

在里面AdminAreaRegistration.vb:

context.MapRoute( _
    "Admin_default", _
    "Admin/{controller}/{action}/{id}", _
    New With {.action = "Index", .id = UrlParameter.Optional}, _
    vbNull, _
    {"PowellCasting.Areas.Admin.Controllers"}
)
Run Code Online (Sandbox Code Playgroud)

假设你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
Run Code Online (Sandbox Code Playgroud)

再次注意到PowellCasting命名空间定义中缺少前缀.