gpt4 book ai didi

asp.net-mvc - 自定义 MVC razor View 引擎的间歇性 View 路径问题

转载 作者:行者123 更新时间:2023-12-02 17:18:51 25 4
gpt4 key购买 nike

我们有一个 Sass 应用程序,我们刚刚实现了一个自定义 razor View 引擎,以便我们可以根据当前登录的用户提供不同的 View 。在 Dev 中,这一切都运行良好。然而,在生产(共享网络主机)中,我们在尝试使用不正确的 View 路径提供不存在的 View 时遇到间歇性问题。

我们部署后,它工作正常。然后大约 20 - 30 分钟后,我们开始收到 View 未找到错误。如果我更改 web.conf 文件以强制应用程序池重新启动,那么一切都会再次正常工作......一段时间。

似乎在某些情况下,FileExists 方法以某种方式为这些路径返回 true。不确定是否是由于缓存问题,或者位于共享主机、网络场、同时对同一页面的多个请求从 FileExists 交叉获取结果等原因???我不知道。

错误:

System.Web.HttpException: The file '/Areas/OrderMgmt/Views/HH/ManageOrders/Pickup.cshtml' does not exist.

在上述情况下,该 View 不存在,它位于 _Base 文件夹中:/Areas/OrderMgmt/Views/HH/ManageOrders/Pickup.cshtml

下面是自定义 View 引擎代码:

{
//http://lonetechie.com/2012/09/25/multi-tenant-architecture-with-asp-net-mvc-4/
public class MulitTenantRazorViewEngine : RazorViewEngine
{
public const string baseFolderPath = "_Base";

public MulitTenantRazorViewEngine()
{
_logger = LogManager.GetLogger(GetType());

AreaViewLocationFormats = new[] {
"~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
"~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
"~/Areas/{2}/Views/%1/Shared/{0}.vbhtml",
"~/Areas/{2}/Views/_Base/{1}/{0}.cshtml",
"~/Areas/{2}/Views/_Base/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/_Base/Shared/{0}.cshtml",
"~/Areas/{2}/Views/_Base/Shared/{0}.vbhtml"
};

AreaMasterLocationFormats = new[] {
"~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
"~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
"~/Areas/{2}/Views/%1/Shared/{0}.vbhtml",
"~/Areas/{2}/Views/_Base/{1}/{0}.cshtml",
"~/Areas/{2}/Views/_Base/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/_Base/Shared/{0}.cshtml",
"~/Areas/{2}/Views/_Base/Shared/{0}.vbhtml"
};

AreaPartialViewLocationFormats = new[] {
"~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
"~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
"~/Areas/{2}/Views/%1/Shared/{0}.vbhtml",
"~/Areas/{2}/Views/_Base/{1}/{0}.cshtml",
"~/Areas/{2}/Views/_Base/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/_Base/Shared/{0}.cshtml",
"~/Areas/{2}/Views/_Base/Shared/{0}.vbhtml"
};

ViewLocationFormats = new[] {
"~/Views/%1/{1}/{0}.cshtml",
"~/Views/%1/{1}/{0}.vbhtml",
"~/Views/%1/Shared/{0}.cshtml",
"~/Views/%1/Shared/{0}.vbhtml",
"~/Views/_Base/{1}/{0}.cshtml",
"~/Views/_Base/{1}/{0}.vbhtml",
"~/Views/_Base/Shared/{0}.cshtml",
"~/Views/_Base/Shared/{0}.vbhtml"
};

MasterLocationFormats = new[] {
"~/Views/%1/{1}/{0}.cshtml",
"~/Views/%1/{1}/{0}.vbhtml",
"~/Views/%1/Shared/{0}.cshtml",
"~/Views/%1/Shared/{0}.vbhtml",
"~/Views/_Base/{1}/{0}.cshtml",
"~/Views/_Base/{1}/{0}.vbhtml",
"~/Views/_Base/Shared/{0}.cshtml",
"~/Views/_Base/Shared/{0}.vbhtml"
};

PartialViewLocationFormats = new[] {
"~/Views/%1/{1}/{0}.cshtml",
"~/Views/%1/{1}/{0}.vbhtml",
"~/Views/%1/Shared/{0}.cshtml",
"~/Views/%1/Shared/{0}.vbhtml",
"~/Views/_Base/{1}/{0}.cshtml",
"~/Views/_Base/{1}/{0}.vbhtml",
"~/Views/_Base/Shared/{0}.cshtml",
"~/Views/_Base/Shared/{0}.vbhtml"
};
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
var PassedController = controllerContext.Controller as BaseController;
Debug.Assert(PassedController != null, "PassedController != null");
return base.CreatePartialView(controllerContext, GetTenantViewPath(partialPath, PassedController));
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var PassedController = controllerContext.Controller as BaseController;
Debug.Assert(PassedController != null, "PassedController != null");
return base.CreateView(controllerContext, GetTenantViewPath(viewPath, PassedController), GetTenantViewPath(masterPath, PassedController));
}

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
var PassedController = controllerContext.Controller as BaseController;
Debug.Assert(PassedController != null, "PassedController != null");
var tenantViewPath = GetTenantViewPath(virtualPath, PassedController);

var isFound = base.FileExists(controllerContext, tenantViewPath);
_logger.Debug(String.Format("Is Found: {0} Path: {1}.", isFound.ToString(), tenantViewPath));

return isFound;
}

private string GetTenantViewPath(string virtualPath, BaseController PassedController)
{
string strReplacementString = "";

if (PassedController == null)
{
strReplacementString = baseFolderPath;
}

else if(PassedController.User == null) {
strReplacementString = baseFolderPath;
}
else
{
strReplacementString = PassedController.User.CurrentAccountCode ?? baseFolderPath;
}


return virtualPath.Replace("%1", strReplacementString);
}

private readonly ILog _logger;
}

}

最佳答案

事实证明,在 Release模式下使用了缓存,这导致了我遇到的问题。您还需要重写 FindView 和 FindPartialView 方法并将 useCache 设置为 false:

//to not used Cached paths. see one of the comments here: http://lonetechie.com/2012/09/25/multi-tenant-architecture-with-asp-net-mvc-4/
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
return base.FindView(controllerContext, viewName, masterName, false);
}

//to not used Cached paths. see one of the comments here: http://lonetechie.com/2012/09/25/multi-tenant-architecture-with-asp-net-mvc-4/
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
return base.FindPartialView(controllerContext, partialViewName, false);
}

关于asp.net-mvc - 自定义 MVC razor View 引擎的间歇性 View 路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29435702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com