2

任何人都知道是否可以将 HomeController 和 Home 相关视图移动到 Areas 目录中?

我试图保持我的根目录干净整洁,并尽可能避免使用 ~/Views 和 ~/Controllers 目录。此外,我可以看到它引起了一些混乱,因为必须解释这些根文件夹仅用于主页内容,而其他所有内容都位于 Areas 文件夹中。我猜这不符合我的组织意识。

我最接近的是使用以下内容进行我的家庭区域路线注册:

context.MapRoute(
    "Home_default",
    "Home/{action}/{id}",
    new { controller="Home", action = "index", id = UrlParameter.Optional }
);

... 但这并不仅仅只是简单的“www.mydomain.com/”。为此,我需要在 Global.asax 中告诉我的“catch all”路由,以某种方式将该请求发送到我的 Home 区域。简单地将 area="Home" 添加到路线数据中是行不通的。“/”的请求仍在我的根目录中寻找 HomeController 和 Views。

有任何想法吗?

4

2 回答 2

0

IIS 可能会在应用您的路由之前将“default.aspx”或某些默认文档名称添加到请求 URL,在这种情况下,您的示例将不起作用。(这可以在 IIS 中配置。)

你需要这样的路线

context.MapRoute(
   "Home_root"
   ,"/"
   ,new { controller="Home", action="index", id=UrlParameter.Optional }
);

但是,当然,路由不能以“/”、“~”等开头。

或者

context.MapRoute(
   "Home_root"
   ,"default.aspx{*parameters}" /// or .htm, .asp, .html or whatever IIS may be adding 
   ,new { controller="Home", action="index", id=UrlParameter.Optional }
);

HTH。赶时间,所以我没有尝试编译任何这些。

于 2010-07-07T18:00:56.703 回答
0
// These additions allow me to route default requests for "~/" to the "home" area
engine.ViewLocationFormats = new string[] { 
    "~/Views/{1}/{0}.aspx",
    "~/Views/{1}/{0}.ascx",
    "~/Areas/{1}/Views/{1}/{0}.aspx", // new
    "~/Areas/{1}/Views/{1}/{0}.ascx", // new
    "~/Areas/{1}/Views/{0}.aspx", // new
    "~/Areas/{1}/Views/{0}.ascx", // new
    "~/Views/{1}/{0}.ascx"
};
于 2010-08-17T20:20:51.127 回答