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:
我正在尝试使用Maarten Balliauw的Domain Route类将子域映射到MVC2应用程序中的区域,以便我有以下URL:
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.
这向我表明MVC只在根视图文件夹中查找视图,而不是在区域内查看视图文件夹。如果我将区域视图文件夹中的视图复制到根视图文件夹,页面呈现正常。然而,这完全违背了将APP划分为区域的目的。
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.
我很困惑为什么它找到区域内的控制器很好,但不是视图。
2 个解决方案
#1
3
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.
好的,我明白了。下载MVC 2源代码并将其添加到我在此处概述的解决方案之后,我逐步完成了MVC代码。我发现区域内的路由实现了IRouteWithArea接口。此接口将“Area”属性添加到RouteData,毫不奇怪,它包含区域的名称。我修改了DomainRoute类,以便实现这个接口,并添加了一些带有这个附加参数的重载构造函数,它现在完全按照我的意愿工作。
The code for registering my route now looks like this:
注册我的路线的代码现在看起来像这样:
context.Routes.Add(
"Admin_Default"
, new DomainRoute(
"admin.mydomain"
,"Admin"
, "{controller}/{action}/{id}"
, new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
));
#2
1
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
.
如果您的区域和默认路由之间有共享控制器名称,并且看起来像这样,则可能需要在调用MapRoute时识别名称空间。
For example, if the top-level namespace of your web application is Web
, the RegisterRoutes
method in Global.asax.cs
file would look something like this:
例如,如果Web应用程序的*命名空间是Web,则Global.asax.cs文件中的RegisterRoutes方法将如下所示:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
null,
new string[] { "Web.Controllers" }
);
and then the RegisterArea
moethod of AdminAreaRegistration.cs
would look something like this:
然后AdminAreaRegistration.cs的RegisterArea moethod看起来像这样:
context.MapRoute(
"Admin_Default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
null,
new string[] { "Web.Areas.Admin.Controllers" }
);
#1
3
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.
好的,我明白了。下载MVC 2源代码并将其添加到我在此处概述的解决方案之后,我逐步完成了MVC代码。我发现区域内的路由实现了IRouteWithArea接口。此接口将“Area”属性添加到RouteData,毫不奇怪,它包含区域的名称。我修改了DomainRoute类,以便实现这个接口,并添加了一些带有这个附加参数的重载构造函数,它现在完全按照我的意愿工作。
The code for registering my route now looks like this:
注册我的路线的代码现在看起来像这样:
context.Routes.Add(
"Admin_Default"
, new DomainRoute(
"admin.mydomain"
,"Admin"
, "{controller}/{action}/{id}"
, new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional }
));
#2
1
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
.
如果您的区域和默认路由之间有共享控制器名称,并且看起来像这样,则可能需要在调用MapRoute时识别名称空间。
For example, if the top-level namespace of your web application is Web
, the RegisterRoutes
method in Global.asax.cs
file would look something like this:
例如,如果Web应用程序的*命名空间是Web,则Global.asax.cs文件中的RegisterRoutes方法将如下所示:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
null,
new string[] { "Web.Controllers" }
);
and then the RegisterArea
moethod of AdminAreaRegistration.cs
would look something like this:
然后AdminAreaRegistration.cs的RegisterArea moethod看起来像这样:
context.MapRoute(
"Admin_Default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
null,
new string[] { "Web.Areas.Admin.Controllers" }
);