我在配置 asp.net mvc 应用程序时遇到问题:
[HttpException (0x80004005): Type 'ASP._Page_Currency_Index_cshtml' does not inherit from 'System.Web.WebPages.WebPage'.]
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +11454171
System.Web.WebPages.BuildManagerWrapper.CreateInstanceOfType(String virtualPath) +347
System.Web.WebPages.VirtualPathFactoryManager.CreateInstanceOfType(String virtualPath) +239
System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath, IVirtualPathFactory virtualPathFactory) +57
System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context) +407
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270
我想做的步骤:
- 将控制器及其视图移动到单独的库中,并将 dll 放入 bin 文件夹中。
创建虚拟文件提供程序和虚拟文件以从程序集中获取数据:
public override Stream Open() { var resourceName = this.path; var asmName = this.assembly.GetName().Name; var manifestName = asmName + ".Views" + resourceName.Trim('~').Replace("/", "."); var resourceStream = this.assembly.GetManifestResourceStream(manifestName); return resourceStream; }
将此行添加到 web.config 中:
<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="System.Web.Helpers" /> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="System.Web.WebPages" /> </namespaces> </pages> </system.web.webPages.razor>
当用户访问此控制器的页面时,它会正确编译为以下类:
public class _Page_Currency_Index_cshtml : System.Web.Mvc.WebViewPage<GridModel<CurrencyModel>> {
之后它因这个错误而失败(正如我所看到的 - 他是对的,类型'ASP._Page_Currency_Index_cshtml'不继承自'System.Web.WebPages.WebPage'。)
我应该怎么做才能让 HttpApplication 知道这个页面应该作为 System.Web.Mvc.WebViewPage 而不是 System.Web.WebPages.WebPage 来管理?
我试图将 Views/Web.Config 移动到 bin 文件夹中,但它没有帮助。