asp.net - 使用预编译的 Razor View 和 VirtualPathProviders 时找不到文件 PrecompiledApp.config

标签 asp.net razor webforms precompiled virtualpathprovider

我们有一个应用程序,它使用 WebForms .aspx 文件来处理几乎所有事情。 Latley 我们一直在使用预编译的 RazorViews 作为获取 nicley 打包功能的一种方式,只需在我们的项目中删除一个新的 dll。但是现在我们发现我们的预编译 View 似乎与我们的 VirtualPathProviders 冲突。

当从外部 dll 加载 VirtualPathProviders 时,应用程序尝试为所有请求加载 PrecompiledApp.config(我们没有得到它)​​。提供程序加载了反射。我们在与注册相同的项目中有一些 VirtualPathProviders,它们工作正常,但是当我们使用 HostingEnvironment.RegisterVirtualPathProvider 从外部 dll 注册提供程序时我们遇到了这个问题。

如果我们添加文件 PrecompiledApp.config,它会尝试获取 _appstart.cshtml 等等。在通过异常(exception)之前,我们必须拥有以下所有这些文件:

  • PrecompiledApp.config
  • _appstart.cshtml
  • _PageStart.cshtml
  • _ViewStart.cshtml
  • View /_ViewStart.cshtml
  • View /共享/_ViewStart.cshtml
  • default.cshtml

  • 我们最终在 default.cshtml 和应用程序的其余部分工作。由于我们希望使用 apsx-files 作为默认值,因此这不是一个可接受的解决方案。我们也担心会出现更多问题,因为我们不知道为什么会这样。

    我们尝试过这种加载提供程序的方式,但仍然出现相同的错误:
    http://sunali.com/2008/01/09/virtualpathprovider-in-precompiled-web-sites/

    异常(exception):
    Could not find file 'C:\MyApp\PrecompiledApp.config'.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\MyApp\PrecompiledApp.config'.
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace: 
    
    
    [FileNotFoundException: Could not find file 'C:\MyApp\PrecompiledApp.config'.]
       System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +12899479
       System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +2481
       System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +229
       System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +102
       System.Web.Hosting.MapPathBasedVirtualFile.Open() +105
       System.Web.WebPages.BuildManagerWrapper.IsNonUpdatablePrecompiledApp() +157
       System.Web.WebPages.BuildManagerWrapper..ctor(VirtualPathProvider vpp, IVirtualPathUtility virtualPathUtility) +48
       System.Web.WebPages.VirtualPathFactoryManager.<.cctor>b__6() +90
       System.Lazy`1.CreateValue() +12776623
       System.Lazy`1.LazyInitValue() +355
       System.Web.WebPages.ApplicationStartPage.ExecuteStartPage(HttpApplication application) +131
       System.Web.WebPages.WebPageHttpModule.StartApplication(HttpApplication application, Action`1 executeStartPage, EventHandler applicationStart) +98
       System.Web.WebPages.WebPageHttpModule.InitApplication(HttpApplication application) +75
       System.Web.WebPages.WebPageHttpModule.Init(HttpApplication application) +268
       System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +575
       System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +352
       System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407
       System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375
    
    [HttpException (0x80004005): Could not find file 'C:\MyApp\PrecompiledApp.config'.]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11700992
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4869221
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
    

    最佳答案

    您确定您的 VirtualPathProvider 的 FileExists 为 PrecompiledApp.config 返回 false 吗?

    IsNonUpdatablePrecompiledApp 标志在实际调用 Open 之前调用 vpp 上的 FileExists,以防止发生异常。

    关于asp.net - 使用预编译的 Razor View 和 VirtualPathProviders 时找不到文件 PrecompiledApp.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428030/

    相关文章:

    gridview - 如何卡住gridview的header【gridview和列宽不固定】

    asp.net - Amazon SES 停止工作

    c# - .net c# asp.net 使文本框文本的一部分加粗

    asp.net-mvc-4 - 如何在 ASP.NET MVC4 中为文本框和按钮继承 CSHTML 文件中的类属性?

    asp.net-mvc - 无法在 Visual Studio 2013 Ultimate Update 5 中使用脚手架创建 MVC View

    javascript - 验证消息在页面加载时显示

    c# - 用户控件事件处理程序在回发时丢失

    javascript - 如何在 JS 或 C# 中使用输入按钮更新 html 表格行的 SQL 数据?

    javascript - 如何从 c# ext.net 调用 javascript

    c# - ASP.NET 向导获取文本框的值