【发布时间】:2015-04-14 09:59:24
【问题描述】:
我正在尝试在自定义路径中使用区域,但遇到了问题。我一直在谷歌上搜索一堆,但还没有找到解决方案。
我的项目是一个 EPiServer CMS 项目(我认为应该没有任何影响,只是想提一下,以防万一)
我的结构是
- 根
- 公司名称
- 区域
- 商业
- 控制器
- 型号
- 观看次数
- 厘米
- 控制器
- 主页控制器
- 型号
- 查看次数
- 首页
- Index.cshtml
- 首页
- 控制器
- 商业
- 区域
- 公司名称
我在 global.asax.cs 中有这个
protected void Application_Start()
{
ViewEngines.Engines.Add(new AreaTemplateViewEngineDynamic());
AreaRegistration.RegisterAllAreas();
...
}
我有一个自定义 RazorEngine(本来可以添加更多默认路径,但现在有这个解决方案)
public class AreaTemplateViewEngineDynamic : RazorViewEngine
{
public AreaTemplateViewEngineDynamic()
{
this.PartialViewLocationFormats = this.ViewLocationFormats = this.MasterLocationFormats =
new string[]
{
"~/CompanyName/Views/{1}/{0}.cshtml", "~/CompanyName/Views/Shared/{0}.cshtml"
};
this.AreaMasterLocationFormats = this.AreaPartialViewLocationFormats = this.AreaViewLocationFormats =
new string[]
{
"~/CompanyName/Areas/{2}/Views/{1}/{0}.cshtml", "~/CompanyName/Areas/{2}/Views/Shared/{0}.cshtml"
};
}
}
添加该区域注册
public class CmsAreaRegistration: AreaRegistration
{
public override string AreaName
{
get { return "Commerce"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Cms_default",
"Cms/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Root.CompanyName.Areas.Cms.Controllers" }
);
}
}
当我尝试加载页面时,它似乎不查看区域路径,只查看非区域路径。
未找到视图“索引”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置:
- ~/Views/HomePage/index.aspx
- ~/Views/HomePage/index.ascx
- ~/Views/Shared/index.aspx
- ~/Views/Shared/index.ascx
- ~/Views/HomePage/index.cshtml
- ~/Views/HomePage/index.vbhtml
- ~/Views/Shared/index.cshtml
- ~/Views/Shared/index.vbhtml
- ~/CompanyName/Views/HomePage/index.cshtml
- ~/CompanyName/Views/Shared/index.cshtml
我希望它找到的路径是
- ~/CompanyName/Areas/Cms/Views/HomePage/index.cshtml
如果我不得不使用
@{Html.RenderAction("MiniCart", "Cart", new { area = "Commerce"} );}
我希望它会找到
- ~/CompanyName/Areas/Commerce/Views/Cart/MiniCart.cshtml
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing asp.net-mvc-areas