【问题标题】:File does not exist when searching custom partial view paths搜索自定义局部视图路径时文件不存在
【发布时间】:2014-10-10 20:29:50
【问题描述】:

我在我们的 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 的问题 - 我已经检查了网站是否正在运行集成模式等...

有什么想法吗?

【问题讨论】:

    标签: asp.net asp.net-mvc iis-8


    【解决方案1】:

    发生这种情况是因为当您将应用程序设置为 debug="false" 时,MVC 进行了优化。一旦我设置了 debug="true",这个错误就消失了。

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多