我正在一个电子商务网站上。该网站连接到多个第三方应用程序,例如Shopify。我为此创建了一个区域,称为B2b(企业对企业)...
我想为B2b
区域下的每个第三方创建一个子文件夹,因此文件夹结构如下所示:
请注意,OrganisationController
对所有第三方都是通用的,因此我没有将其放在任何子文件夹中。
这是我的地区注册代码:
public class B2bAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "B2b"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Shopify_default",
"B2b/Shopify/{controller}/{action}/{id}",
new { controller = "Dashboard", action = "DisplayProducts", id = UrlParameter.Optional },
new[] { "e-commerce.Web.Areas.B2b.Controllers.Shopify" }
);
context.MapRoute(
"B2b_default",
"B2b/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
现在,如果我尝试以下URL:
https://localhost:44339/b2b/shopify/setup/install?shop=some-name
它将击中正确的控制器:
[HttpGet]
public ActionResult Install(string shop)
{
var myViewModel = new MyViewModel(shop);
return View(myViewModel);
}
但是视图引擎无法找到正确的视图,这是我得到的错误:
您会注意到,View Engine不在搜索Shopify
子文件夹。我想我可以通过返回视图路径来解决此问题,但是我想知道是否有更优雅的解决方案?
我借助this tutorial解决了该问题
我创建了自定义的Razor View Engine:
public class ExpandedViewEngine : RazorViewEngine
{
public ExpandedViewEngine()
{
var thirdPartySubfolders = new[]
{
"~/Areas/B2b/Views/Shopify/{1}/{0}.cshtml"
};
ViewLocationFormats = ViewLocationFormats.Union(thirdPartySubfolders).ToArray();
// use the following if you want to extend the partial locations
// PartialViewLocationFormats = PartialViewLocationFormats.Union(new[] { "new partial location" }).ToArray();
// use the following if you want to extend the master locations
// MasterLocationFormats = MasterLocationFormats.Union(new[] { "new master location" }).ToArray();
}
}
并且已将网站配置为在Global.asax
中使用上述视图引擎:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ViewEngines.Engines.Add(new ExpandedViewEngine());
AreaRegistration.RegisterAllAreas();
// more configuratins...
}
}