Razor 编译期间忽略继承 Web.config 中的命名空间
Namespaces in Inherited Web.config ignored during Razor compilation
我有一个使用 MVC4 的 Sitefinity 项目,在根 Views 文件夹的子文件夹中继承了 web.config 文件,这些文件导入特定于控制器模型的命名空间。这以前是有效的,我不知道发生了什么变化,但它不再有效了,我只能挠头了。设置类似于:
- Views
- MyController
- MyView.cshtml
- Web.Config
然后在 web.config 我有:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MyController.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
这很简单。在我对该子文件夹的看法中,我得到了我的 class 的完整 Intellisense 识别(悬停在 @model MyModel
上说 class MyController.Models.MyModel ...
所以我知道 VS 正在获取额外的命名空间)。
但是当我编译项目并请求那个页面时,我得到一个异常:
Type : System.Web.HttpCompileException, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Message : e:\TEMP\rootd89ea4\b5299118\App_Web_index.cshtml#mylayout.cshtml.11a9a948.hv-sp9as.0.cs(160):
error CS0246: The type or namespace name 'MyModel' could not be found (are you missing a using directive or an assembly reference?)
TargetSite : System.CodeDom.Compiler.CompilerResults Compile()
Stack Trace :
at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath)
at System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
at System.Web.Mvc.VirtualPathProviderViewEngine.<>c__DisplayClass4.<GetPathFromGeneralName>b__0(String path)
at System.Web.WebPages.DefaultDisplayMode.GetDisplayInfo(HttpContextBase httpContext, String virtualPath, Func`2 virtualPathExists)
at System.Web.WebPages.DisplayModeProvider.<>c__DisplayClassb.<GetDisplayInfoForVirtualPath>b__8(IDisplayMode mode) ...
因此,如果我进入并打开 ASP.NET 临时目录中的已编译页面文件,我可以看到我的命名空间未包含在 using
语句中,因此 "type or namespace could not be found"异常。
可能发生了什么导致它停止工作?我该如何解决?也有其他开发人员参与该项目,但在比较了修订历史之后,似乎并没有什么不同。我很茫然。
这被追溯到与 Sitefinity 相关的扩展 Feather 中的错误。 github 问题可以在这里查看:
Namespaces in Inherited Web.config ignored during Razor compilation #2190
此外,关于此问题的 Sitefinity 论坛 post 可在此处找到:
Namespaces in Inherited Web.config ignored during Razor compilation
我有一个使用 MVC4 的 Sitefinity 项目,在根 Views 文件夹的子文件夹中继承了 web.config 文件,这些文件导入特定于控制器模型的命名空间。这以前是有效的,我不知道发生了什么变化,但它不再有效了,我只能挠头了。设置类似于:
- Views
- MyController
- MyView.cshtml
- Web.Config
然后在 web.config 我有:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MyController.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
这很简单。在我对该子文件夹的看法中,我得到了我的 class 的完整 Intellisense 识别(悬停在 @model MyModel
上说 class MyController.Models.MyModel ...
所以我知道 VS 正在获取额外的命名空间)。
但是当我编译项目并请求那个页面时,我得到一个异常:
Type : System.Web.HttpCompileException, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Message : e:\TEMP\rootd89ea4\b5299118\App_Web_index.cshtml#mylayout.cshtml.11a9a948.hv-sp9as.0.cs(160):
error CS0246: The type or namespace name 'MyModel' could not be found (are you missing a using directive or an assembly reference?)
TargetSite : System.CodeDom.Compiler.CompilerResults Compile()
Stack Trace :
at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath)
at System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
at System.Web.Mvc.VirtualPathProviderViewEngine.<>c__DisplayClass4.<GetPathFromGeneralName>b__0(String path)
at System.Web.WebPages.DefaultDisplayMode.GetDisplayInfo(HttpContextBase httpContext, String virtualPath, Func`2 virtualPathExists)
at System.Web.WebPages.DisplayModeProvider.<>c__DisplayClassb.<GetDisplayInfoForVirtualPath>b__8(IDisplayMode mode) ...
因此,如果我进入并打开 ASP.NET 临时目录中的已编译页面文件,我可以看到我的命名空间未包含在 using
语句中,因此 "type or namespace could not be found"异常。
可能发生了什么导致它停止工作?我该如何解决?也有其他开发人员参与该项目,但在比较了修订历史之后,似乎并没有什么不同。我很茫然。
这被追溯到与 Sitefinity 相关的扩展 Feather 中的错误。 github 问题可以在这里查看:
Namespaces in Inherited Web.config ignored during Razor compilation #2190
此外,关于此问题的 Sitefinity 论坛 post 可在此处找到:
Namespaces in Inherited Web.config ignored during Razor compilation