asp.net-mvc2 - 返回 View() 时控制器不考虑区域
当在驻留在某个区域的控制器中调用return View()
时,它会尝试在主~/Views/{Controller}/
文件夹和主< code>~/Views/Shared/ 文件夹。它不会在 ~/{Area}/Views/{Controller}/
文件夹中查找。
我尝试将 area
的路线值添加到 MapRoute
函数,还尝试将“area”的“datatoken”添加到路线的 DataTokens
属性。
我在这里错过了什么吗?
这是 MapRoute 调用:
routes.MapRoute("Product", "Products/{GroupName}/{CategoryId}/{CategoryName}/{ProductId}/{ProductName}/{PageName}", New With {.Area = "Products", .controller = "Products", .action = "Product", .PageName = ""}, New With {.CategoryId = "[0-9]*", .ProductId = "[0-9]*"})
When calling return View()
in a controller that resides in an area it tries to locate the views in the main ~/Views/{Controller}/
folder and the main ~/Views/Shared/
folder. It doesn't look in the ~/{Area}/Views/{Controller}/
folder.
I tried adding a routevalue for area
to the MapRoute
function and also tried adding a 'datatoken' for "area" to the Route's DataTokens
property.
Am I missing something here?
This is the MapRoute
call:
routes.MapRoute("Product", "Products/{GroupName}/{CategoryId}/{CategoryName}/{ProductId}/{ProductName}/{PageName}", New With {.Area = "Products", .controller = "Products", .action = "Product", .PageName = ""}, New With {.CategoryId = "[0-9]*", .ProductId = "[0-9]*"})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您好,要查看的一般内容:
您是否注册了区域映射和区域映射?
两个位置的控制器名称相同吗?
global.asax 中的区域映射示例
参见行:AreaRegistration.RegisterAllAreas(); // 这是您需要做的部分
}
并且每个区域都必须在某处设置映射或映射文件。
此处的示例
名称空间项目
{
///
/// 管理区域注册
///
公共类 AdministratorAreaRegistration :AreaRegistration
{
///
/// 获取要注册的区域的名称。
///
/// 要注册的区域的名称。
公共覆盖字符串区域名称
{
得到
{
返回“管理员”;
}
您
如果
的路由有问题,请检查此。
我建议使用本指南,并为每个路由创建测试:
http://haacked.com/archive /2007/12/17/testing-routes-in-asp.net-mvc.aspx
Hi general stuff to look at:
Have you registered area mapping and area mapping?
have you same name for controller in both locations?
Example of area mapping in global.asax
see line : AreaRegistration.RegisterAllAreas(); // This is part you neeed to do
}
and every area has to have mapping or mapping file somewhere set up.
Example here
namespace Project
{
///
/// Registration of administration area
///
public class AdministratorAreaRegistration : AreaRegistration
{
///
/// Gets the name of the area to be registered.
///
/// The name of the area to be registered.
public override string AreaName
{
get
{
return "Administrator";
}
}
}
if you have problem with the routing check this.
I would recomend to use this guide, and create tests for every routing:
http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx
正如我们通常对区域所做的那样,我们添加一个 AeaRegistraion 文件 (FooAreaRegistration.cs) 并重写 AreaName 和 RegisterArea() 方法。我们将 AreaName 设置为 AearRegistration 文件所在文件夹相对于全局应用程序的相对路径。以下是示例。
As we usually do with Areas, we add an AeaRegistraion file (FooAreaRegistration.cs) and override AreaName and RegisterArea() method. AreaName we set to the relative path of the folder where AearRegistration file resides with respect to the global application. Following is example.