路由是在我的区域中找到控制器,但不是视图
我正在尝试使用 Maarten Balliauw 的 域路由< /a> 类将子域映射到 MVC2 应用程序中的区域,以便我拥有类似以下的 URL:
http://admin.mydomain.com/home/index
而不是:
http://mydomain.com/admin/home/index
到目前为止,我只取得了部分成功。执行被路由到正确区域中的正确控制器,但无法找到正确的视图。我收到以下错误:
The view 'Index' or its master was not found. The following locations were searched:
~/Views/AdminHome/Index.aspx
~/Views/AdminHome/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
这表明 MVC 仅在根视图文件夹中查找视图,而不是区域内的视图文件夹。如果我将视图从区域的视图文件夹复制到根视图文件夹,页面将呈现良好。但这完全违背了APP划分Area的初衷。
我将该区域的路线定义为:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.Add(
"Admin_Default"
, new DomainRoute(
"admin.localhost"
, "{controller}/{action}/{id}"
, new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
));
}
}
我很困惑为什么它可以在区域内找到控制器,而不是视图。
I'm trying to use Maarten Balliauw's Domain Route class to map sub-domains to the areas in an MVC2 app so that I have URLs like:
http://admin.mydomain.com/home/index
instead of:
http://mydomain.com/admin/home/index
So far, I've only had partial success. Execution is being routed to the correct controller in the correct area, but it cannot then find the correct view. I'm receiving the following error:
The view 'Index' or its master was not found. The following locations were searched:
~/Views/AdminHome/Index.aspx
~/Views/AdminHome/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
This indicates to me that MVC is looking for the view only in the root views folder and not the views folder within the Area. If I copy the view from the Area's views folder to the root views folder, the page renders fine. This however, completely defeats the purpose of dividing the APP into Areas.
I'm defining the route for the area as:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.Add(
"Admin_Default"
, new DomainRoute(
"admin.localhost"
, "{controller}/{action}/{id}"
, new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
));
}
}
I'm confused as to why it is finding the controller within the Area fine, but not the view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我明白了。下载 MVC 2 源代码并将其添加到我的解决方案后,如下所述 此处,我单步调试了 MVC 代码。我发现区域内的路由实现了 IRouteWithArea 接口。该接口向 RouteData 添加了一个“Area”属性,毫不奇怪,该属性包含区域的名称。我修改了 DomainRoute 类来实现此接口,并添加了几个采用此附加参数的重载构造函数,它现在完全按照我想要的方式工作。
注册路线的代码现在如下所示:
OK, I figured it out. After downloading the MVC 2 source code and adding it to my solution as outlined here, I stepped through the MVC code. I found that Routes within areas implement the IRouteWithArea interface. This interface adds an 'Area' property to the RouteData which, not surprisingly, contains the area's name. I modified the DomainRoute class so to implement this interface and added a couple of overloaded constructors that took this additional parameter, and it now works exactly as I wanted it to.
The code for registering my route now looks like this:
如果您的区域和默认路由之间共享控制器名称(而且看起来确实如此),那么您可能需要在调用
MapRoute
时识别命名空间。例如,如果您的 Web 应用程序的顶级命名空间是
Web
,则Global.asax.cs
文件中的RegisterRoutes
方法将看起来像这样像这样:然后
AdminAreaRegistration.cs
的RegisterArea
方法看起来像这样:If you have share controller names between your areas and your default routes, and it looks like you do, you may need to identify namespaces when you call
MapRoute
.For example, if the top-level namespace of your web application is
Web
, theRegisterRoutes
method inGlobal.asax.cs
file would look something like this:and then the
RegisterArea
moethod ofAdminAreaRegistration.cs
would look something like this: