我的MVC应用程序出现了一个奇怪的问题。我们继承RazorViewEngine来创建自定义视图引擎,以方便视图排列中的自定义逻辑。
我们有一个潜在的观看路径列表:
PartialViewLocationFormats = new[]
{
"~/Views/Partial/Shared/Base/$thing/{0}.$otherthing.cshtml",
"~/Views/Partial/Shared/Base/$thing/{0}.cshtml",
"~/Views/Partial/Shared/Base/{0}.$otherthing.cshtml",
"~/Views/Partial/Shared/Base/{0}.cshtml"
};
然后我们覆盖FileExists方法,如:
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
return base.FileExists(controllerContext, this.ParsePath(controllerContext, virtualPath));
}
ParsePath方法如下:
private string ParsePath(ControllerContext controllerContext, string virtualPath)
{
string newPath = virtualPath;
BaseController controller = controllerContext.Controller as BaseController;
if (controller != null)
{
if (controller.Model != null)
{
if (!string.IsNullOrEmpty(controller.Model.Thing))
{
newPath = newPath.Replace("$thing", controller.Model.Thing);
}
if (!string.IsNullOrEmpty(controller.Model.OtherThing))
{
newPath = newPath.Replace("$otherthing", controller.Model.OtherThing);
}
}
}
return newPath;
}
这在本地工作正常,但在发布到Win 2012,IIS8框后,我看到以下错误:
文件'/Views/Partial/Shared/Base/Footer.blar.cshtml'不存在。
TargetSite: System.Web.Compilation.BuildResult GetVPathBuildResultInternal(System.Web.VirtualPath, Boolean, Boolean, Boolean, Boolean, Boolean)
StackTrace: at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) yadda yadda yadda
'/ Views / Partial / Shared / Base / Footer.cshtml'确实存在,为什么会抛出异常?
我的预感是代码很好,它是IIS的一个问题 - 我已经检查过这些网站正在运行集成模式等...
有什么想法吗?
答案 0 :(得分:1)
这种情况正在发生,因为当您将应用程序设置为debug =" false"时,MVC会进行优化。一旦我设置debug =" true",这个错误就消失了。