asp.net-mvc - 无法加载文件或程序集 'System.Web.WebPages.Razor'?

标签 asp.net-mvc azure razor asp.net-mvc-5

我有一个 MVC5 应用程序,正在尝试将其发布到 Microsoft Azure 虚拟主机。当我在本地主机上执行应用程序时,一切都按预期进行。但是,当我发布到 Azure 并尝试加载主机域时,我收到以下错误消息:

Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) .

当我查看我的 Web.Config 文件时,我有以下内容:

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup>

如果我从错误消息中收集到的内容是正确的,则我的应用程序或某些组件正在寻找版本 2.0.0.0而不是3.0.0.0 ,但我不知道是什么/哪些可能导致问题。

还有其他人在发布到 Azure 时遇到过这种情况吗?

编辑:

根据trailmax的建议我表演了Add-BindingRedirect在包管理器控制台中,结果如下:

PM> Add-BindingRedirect
Name                 OldVersion        NewVersion
--------------       ------------      -------------
System.Web.Mvc       0.0.0.0-5.1.0.0   5.1.0.0
System.Web.WebPages  0.0.0.0-3.0.0.0   3.0.0.0

然后,我检查了 localhost 以验证一切是否仍处于工作状态(检查),并尝试再次发布到 Azure。还是同样的问题:/

以下是完整的分割:

Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 
  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.FileLoadException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

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.  

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Stack Trace:  [FileLoadException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
   Microsoft.Web.WebPages.OAuth.PreApplicationStartCode.Start() +0

[InvalidOperationException: The pre-application start initialization method Start on type Microsoft.Web.WebPages.OAuth.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +547
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +521

[HttpException (0x80004005): The pre-application start initialization method Start on type Microsoft.Web.WebPages.OAuth.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930568
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

编辑2:

更正,主根Web.config文件修改自Add-BindingRedirect 。在发布时仍然收到相同的问题。

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

最佳答案

您的一个组件依赖于 v2,因此出现了问题。您需要在 web.config 中添加绑定(bind)重定向,以使程序集指向 v3。

您可以使用命令在 nuget 控制台中执行此操作

PM> Add-BindingRedirect

如果该工具无法检测到版本不匹配的情况,您可以手动将其添加到web.config:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

找出哪些组件依赖于旧版本的方法之一是使用依赖项跟踪器工具。我过去用过的那个叫CheckAsm 。免费版本足够使用一两次。

关于asp.net-mvc - 无法加载文件或程序集 'System.Web.WebPages.Razor'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868449/

相关文章:

azure - 什么会阻止 Azure Function Runtime 在 ILB ASE 部署中启动?

asp.net - Azure应用程序服务-如何使Always On适用于特定应用程序?

asp.net-mvc-3 - 如何在 ASP.NET MVC 3 Preview 1 的 Razor 中指定默认布局页面?

javascript - 准备在属性中使用的转义/编码字符

c# - 我如何在我的 ASP MVC 应用程序的 View 中调用 javascript 函数

asp.net - 在 MVC View 中添加具有散列名称的 js 文件(由 Angular CLI 生成)

python-3.x - 从 Python 中的 HTTP 触发的 azure 函数返回 excel 文件

css - 使用@Html.EditorForModel 的限制

c# - 启用跨域 ASP.net Web API

.net - ASP.NET MVC : OutputCache and http headers