1

我有一个 ASP.NET 站点,其中包含 WebForms 和 MVC 部分。当我尝试预编译该站点时,除了从 App_Themes 下提供图像/css 之外,一切正常。

如果我请求类似的东西/foo/App_Themes/themeName/my.png,我会收到此错误:

The file '/foo/App_Themes/themeName/my.png.cshtml' is in the special directory 'App_Themes', which is not allowed.

我只在预编译时得到这个,否则它工作正常。大概 MVC 路由以某种方式干扰,但我不知道为什么或如何禁用它。

如果它有帮助,这里是堆栈跟踪:

System.Web.Compilation.BuildManager.ValidateVirtualPathInternal(VirtualPath virtualPath, Boolean allowCrossApp, Boolean codeFile) +9930801
System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +455
System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +103
System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +165
System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound) +33
System.Web.WebPages.BuildManagerWrapper.GetObjectFactory(String virtualPath) +26
System.Web.WebPages.BuildManagerWrapper.ExistsInPrecompiledSite(String virtualPath) +80
System.Web.WebPages.BuildManagerWrapper.Exists(String virtualPath) +13
System.Web.WebPages.<>c__DisplayClass1.<Exists>b__0(IVirtualPathFactory factory) +15
System.Linq.Enumerable.Any(IEnumerable`1 source, Func`2 predicate) +146
System.Web.WebPages.VirtualPathFactoryManager.Exists(String virtualPath) +73
System.Web.WebPages.DefaultDisplayMode.GetDisplayInfo(HttpContextBase httpContext, String vir tualPath, Func`2 virtualPathExists) +42
System.Web.WebPages.<>c__DisplayClassb.<GetDisplayInfoForVirtualPath>b__8(IDisplayMode mode) +22
System.Linq.WhereSelectListIterator`2.MoveNext() +104
System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate) +94
System.Web.WebPages.DisplayModeProvider.GetDisplayInfoForVirtualPath(String virtualPath, HttpContextBase httpContext, Func`2 virtualPathExists, IDisplayMode currentDisplayMode, Boolean requireConsistentDisplayMode) +204
System.Web.WebPages.WebPageRoute.GetRouteLevelMatch(String pathValue, IEnumerable`1 supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModeProvider) +201
System.Web.WebPages.WebPageRoute.MatchRequest(String pathValue, IEnumerable`1 supportedExtensions, IVirtualPathFactory virtualPathFactory, HttpContextBase context, DisplayModeProvider displayModes) +281
System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context) +235
System.Web.WebPages.WebPageHttpModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +89
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69 
4

1 回答 1

2

我遇到了同样的问题,但它只发生在我发布我的 Web 应用程序并且在本地可以正常工作时。我想出了两个潜在的解决方案。

哈克解决方案

通过查看源代码,我可以看到它抛出了这个错误,因为“BuildManager”对象有一个字段“_forbiddenTopLevelDirectories”,如果路径位于许多特殊目录中,它将检查并抛出一个期望,“App_Themes”是一个其中。

Application_Start因此,通过使用反射,我通过在全局文件方法中调用以下代码从集合中删除“App_Themes”对象。

        var buildManagerType = typeof(BuildManager);
        var theBuilderManager = buildManagerType.GetField("_theBuildManager"
             , BindingFlags.Static | BindingFlags.NonPublic)
             .GetValue(null);

        var forbiddenDirectoryField = buildManagerType
        .GetField("_forbiddenTopLevelDirectories",
        BindingFlags.Instance | BindingFlags.NonPublic)
       .GetValue(theBuilderManager);

        var removeMethod = forbiddenDirectoryField.GetType().GetMethod("Remove");

        removeMethod.Invoke(forbiddenDirectoryField, new object[] { "App_Themes" });

这解决了这个问题,我的 css + 图像文件现在可以在部署时正确提供。警告- 我对编辑字段的后果没有很好的理解,_forbiddenTopLevelDirectories这样做可能会产生意想不到的副作用。

替代解决方案

我注意到的另一件事是,当 webconfig 中模块节点的 runAllManagedModulesForAllRequests 属性设置为 false 时,我不会收到错误消息。因此,如果您的应用程序可以将此设置为 false 考虑此解决方案

<modules runAllManagedModulesForAllRequests="false">

于 2017-01-03T16:36:37.590 回答