VB 中的 ASP NET MVC 区域路由/多路由问题
我对 .net 非常缺乏经验,并且刚刚开始学习 MVC。我遇到了有关找到多个控制器的问题:
“找到了与名为“reviews”的控制器匹配的多种类型。如果服务此请求的路由 ('{controller}/{action}/{ id}') 未指定命名空间来搜索与请求匹配的控制器。如果是这种情况,请通过调用采用“namespaces”参数的“MapRoute”方法的重载来注册此路由。”
我最近添加了我的应用程序有一个新的“管理”区域,其中有一个“ReviewController”。主应用程序文件夹中还有一个“ReviewController”:
啊 - 作为新用户,我无法发布图像,但基本上我在“Controllers”和“Areas/Admin/Contollers”中有一个“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
在阅读了我遇到的问题后,我添加了一些代码位:
我的管理控制器具有正确的命名空间定义的命名
- 空间 PowellCasting.Areas.Admin 而不是简单的 PowellCasting。
- 我在全局
- ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers") 中设置了 RegisterAllAreas 来指定默认路由。
我现在遇到的具体问题是,当我转到“/Reviews”时,我收到上面显示的多个控制器错误,具体来说:
*“reviews”的请求已找到以下匹配的控制器: PowellCasting.PowellCasting.Areas.Admin.ReviewsController
PowellCasting.PowellCasting.ReviewsController*
我已启用路由调试器,并且仅显示一个匹配项:
啊 - 作为新用户,我无法发布图像,但它显示:
Admin/{controller }/{action}/{id} as FALSE
and
{controller}/{action}/{id} as 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
没有成功。
我确信这应该很简单,而且我已经尝试了很多方法 - 这非常令人沮丧。任何帮助将不胜感激。
我在这里发表的第一篇文章 - 嗨! :)
I'm pretty inexperienced with .net and have just started learning MVC. I've hit an issue concerning multiple controllers being found:
"Multiple types were found that match the controller named 'reviews'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."
I've recently added a new "Admin" area to my app and within that I have a "ReviewController". There is also a "ReviewController" within the main app folder:
ah - as a new user I can't post an image, but basically I have a "ReviewController" in "Controllers" and in "Areas/Admin/Contollers".
I have 2 routes set up so far:
Default route in 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
Area route in 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
After reading around the issues I was having, I have added a number of bits of code:
My Admin controllers have the right namespace defined
- Namespace PowellCasting.Areas.Admin rather than simply PowellCasting.
- I have RegisterAllAreas set in the global
- ControllerBuilder.Current.DefaultNamespaces.Add("PowellCasting/Controllers") is in place to specify the default route.
The specific problem I have now is that when I go to "/Reviews" I get the multiple controllers error shown above, specifically:
*The request for 'reviews' has found the following matching controllers:
PowellCasting.PowellCasting.Areas.Admin.ReviewsController
PowellCasting.PowellCasting.ReviewsController*
I've enabled the route debugger and that only shows one match:
ah - as a new user I can't post an image but it shows:
Admin/{controller}/{action}/{id} as FALSE
and
{controller}/{action}/{id} as TRUE
This is as expected so I don't know why I'm receiving the issue.
I have read about overloading the maproute method with the namespace, but couldn't find an example in VB (loads in c#). But I tried this:
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
and
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
but without success.
I'm sure this should be straightforward and I've tried a number of things - it very frustrating. Any help would be really appreciated.
My first post on here - Hi! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您仔细阅读您收到的错误消息:
您会注意到
PowellCasting
重复了两次。因此,在您的主要 Global.asax 路由注册中:
假设根目录中的
ReviewController
定义如下:请注意,命名空间定义中缺少
PowellCasting
前缀(即一个自动添加它的 VB subtility,我想这是您的应用程序的名称)和
AdminAreaRegistration.vb
内部:它假设您的
ReviewController
在此区域中已定义 再次注意命名空间定义中缺少
PowellCasting
前缀。If you read carefully the error message you are getting:
you will notice that
PowellCasting
is repeated twice.So in your main Global.asax route registration:
this assumes that your
ReviewController
in the root is defined like this:Notice the absence of the
PowellCasting
prefix in the namespace definition (that's a VB subtility which adds it automatically, I suppose that's the name of your application)and inside the
AdminAreaRegistration.vb
:which assumes that your
ReviewController
in this area is defined like soOnce again notice the absence of the
PowellCasting
prefix in the namespace definition.DealerNetworksAreaRegistration.cs:
//添加区域
RouteConfig.cs
Global.asax.cs
DealerNetworksAreaRegistration.cs :
//Add Area
RouteConfig.cs
Global.asax.cs
我也有类似的问题。
我有一个应用程序,其中有两个区域: Common 、 Products
当我打开应用程序时,正在调用 Common_default 路由,但是当我看到 RoutesCollection.Routes 时,它向我显示了 4 条路由。实际上我只定义了 2 条路线 - 一条用于 common ,一条用于 Products 。
阅读上述评论后,我只是尝试更改我的网站的项目属性。
我将默认命名空间从 "MyWebsite.Web" 更改为 "MyWebsite" 。它成功了。我的应用程序已启动并正在运行。
底线是永远不要有“。”在 MVC4 网站项目的默认命名空间中。
I too had a similar problem.
I had an application where i had two areas : Common , Products
And when i opened the application , the Common_default route was being called, but when i saw the RoutesCollection.Routes, it showed me 4 routes. Actually i defined only 2 routes - one for common , one for Products.
After reading the above comments, i just gave it a try my changing the project properties for my website.
I changed the default namespace from "MyWebsite.Web" to "MyWebsite" . And it did the trick. My applciation was up and running.
The bottomline is never have "." in the default namespace for your MVC4 website project.