【问题标题】:Using MVC 4 areas at custom path在自定义路径中使用 MVC 4 区域
【发布时间】: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


    【解决方案1】:

    我编写了自己的 RazorViewEngine,在其中添加了一些自定义代码来查找路径。 可以直接使用 URL,因为 URL 是 CMS 控制的,所以 URL 不代表 MVC 路径。

    【讨论】:

      【解决方案2】:

      当您还应该设置以下位置时,您只是在设置 AreaMasterLocation 的位置:

      • AreaPartialViewLocationFormats
      • AreaViewLocationFormats

      在对象浏览器中找到以下类:VirtualPathProviderViewEngine 以获得更多属性和方法。

      【讨论】:

        猜你喜欢
        • 2013-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        相关资源
        最近更新 更多