运行时错误:无法加载文件或程序集'System.Web.WebPages.Razor,版本= 3.0.0.0'

30

我通过NuGet调整了我的ASP.NET MVC4包,现在System.Web.WebPages.Razor v3无法加载。有时其他包也无法加载。

我的同事可以毫无问题地运行该解决方案,因此问题肯定是出现在我的开发系统配置上(而不是已经检入的任何内容)。

我已经尝试了各种SOf和其他论坛链接中的建议,包括:

此外,还有从那里链接的建议。我还检查了控制面板,没有安装引用MVC的程序。(如果重要的话,我正在运行MS Win 7 SP 1)。

我接受所有建议。

以下是我的核心转储信息:

Server Error in '/' Application.

Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

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 load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

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=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

=== Pre-bind state information === LOG: User = CodesWithHammer LOG: DisplayName = System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///c:/users/CodesWithHammer/documents/visual studio 2012/Projects/MPP/MPP/ LOG: Initial PrivatePath = c:\users\CodesWithHammer\documents\visual studio 2012\Projects\MPP\MPP\bin Calling assembly : Microsoft.Web.WebPages.OAuth, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

=== LOG: This bind starts in default load context. LOG: Using application configuration file: c:\users\CodesWithHammer\documents\visual studio 2012\Projects\MPP\MPP\web.config LOG: Using host configuration file: C:\Users\CodesWithHammer\Documents\IISExpress\config\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting download of new URL file:///C:/Users/CodesWithHammer/AppData/Local/Temp/Temporary ASP.NET Files/root/5bf3ca07/f960d9c5/System.Web.WebPages.Razor.DLL. LOG: Attempting download of new URL file:///C:/Users/CodesWithHammer/AppData/Local/Temp/Temporary ASP.NET Files/root/5bf3ca07/f960d9c5/System.Web.WebPages.Razor/System.Web.WebPages.Razor.DLL. LOG: Attempting download of new URL file:///c:/users/CodesWithHammer/documents/visual studio 2012/Projects/MPP/MPP/bin/System.Web.WebPages.Razor.DLL. LOG: Attempting download of new URL file:///c:/users/CodesWithHammer/documents/visual studio 2012/Projects/MPP/MPP/bin/System.Web.WebPages.Razor/System.Web.WebPages.Razor.DLL. LOG: Attempting download of new URL file:///C:/Users/CodesWithHammer/AppData/Local/Temp/Temporary ASP.NET Files/root/5bf3ca07/f960d9c5/System.Web.WebPages.Razor.EXE. LOG: Attempting download of new URL file:///C:/Users/CodesWithHammer/AppData/Local/Temp/Temporary ASP.NET Files/root/5bf3ca07/f960d9c5/System.Web.WebPages.Razor/System.Web.WebPages.Razor.EXE. LOG: Attempting download of new URL file:///c:/users/CodesWithHammer/documents/visual studio 2012/Projects/MPP/MPP/bin/System.Web.WebPages.Razor.EXE. LOG: Attempting download of new URL file:///c:/users/CodesWithHammer/documents/visual studio 2012/Projects/MPP/MPP/bin/System.Web.WebPages.Razor/System.Web.WebPages.Razor.EXE.

Stack Trace:

[FileNotFoundException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral,

PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.] 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=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..] System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection1 methods, Func1 setHostingEnvironmentCultures) +550 System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath) +90 System.Web.Compilation.BuildManager.ExecutePreAppStart() +135 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[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=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9874840 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055

(针对那些阅读到这里的人:我没有足够的声望来制作标签 pulling-out-my-hair 。如果有适当的现有标签,请包含在内。KTHX。)
注:当我尝试手动更新 Microsoft.AspNet.Mvc 到版本3时,在Package Manager Console中出现以下错误:
安装失败。回滚... Install-Package:更新'Microsoft.AspNet.Mvc 5.0.0'到'Microsoft.AspNet.Mvc 3.0.20105.1'失败。无法找到与'Microsoft.AspNet.Mvc 3.0.20105.1'兼容的'Microsoft.AspNet.Mvc.FixedDisplayModes'版本。 在第1行第16个字符处: + Install-Package <<<< Microsoft.AspNet.Mvc -Version 3.0.20105.1 + CategoryInfo:NotSpecified:(:) [Install-Package],InvalidOperationException + FullyQualifiedErrorId:NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
ETA2:我从我的同事那里得到了包目录。他们有“MS.AspNet.Mvc”v5,并且运行良好。但是他们还有“MS.AspNet.Mvc.FixedDisplayModes”v5。所以我要添加这两个。是的,我已经备份了整个解决方案。 :)
ETA3:因此,我重新升级为AspNet MVC v5,并跳过了已弃用的Mvc.FixedDisplayNodes。然后,我遇到了AspNet.WebPages版本冲突的问题,所以我使用了这个解决方法:检测到ASP.NET Web Pages的版本冲突:指定版本为“1.0.0.0”,但bin中的版本为“2.0.0.0”在ASP.MET MVC 3中 现在我的项目可以运行了!
感谢所有人的帮助!

你尝试过使用 clean 命令清理解决方案,然后再进行 rebuild 吗?问题可能是你通过 NuGet 从 MVC4 更新到了 MVC5。 - Stan
我反复进行了“清理”和“重建”。我没有看到任何MVC5包,但我会再次检查。 - Codes with Hammer
12个回答

38
你可以在Nuget包Microsoft ASP.NET Web Pages版本3.2.0中找到它。
如果你引用的是3.0.0.0之前的版本,请删除引用,添加正确.dll文件的引用到你的packages文件夹中,并确保在.dll文件的属性中将“Copy Local”设置为“True”。
然后,在你的web.config文件中添加内容(如@MichaelEvanchik所提到的)。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" PublicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
  </dependentAssembly>
</assemblyBinding>

是的,这适用于“无法加载文件或程序集'System.Web.WebPages.Razor,Version=3.0.0.0”的错误。 - Tom Stickel
是的,兄弟,它可以工作。 非常感谢您的支持。 :) - Unknown_Coder
非常感谢您! - shim-kun

24
"Update-Package –reinstall Microsoft.AspNet.WebPages"

请在包管理器控制台中使用以下命令重新安装 Microsoft.AspNet.WebPages NuGet 包。

有没有办法收藏一条评论?因为这太棒了,而且这个问题经常困扰我! - user3654055
@user3654055,这是链接:https://dev59.com/Geo6XIcBkEYKwwoYLhPO - Christian

4

我也遇到了与NUGET相关的同样问题。

以下标签有所帮助:

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

如果这个问题出现在服务器上,我需要确认运行应用程序池的帐户对文件系统具有更高的“特权”,但我不认为这是你在这里遇到的问题。


你确定旧版本1到4吗?我只是再次确认,因为我知道一个字符的拼写错误会带来什么后果。 - Codes with Hammer
这是一个不好的建议。首先,他已经试图加载v3,但找不到它,所以没有理由重定向到v3。而且,通常将程序集重定向到旧版本是一个不好的想法。不过,从v3重定向到v4确实是有意义的,我认为甚至官方支持这样做。 - fejesjoco
我在你发表评论之前就已经编辑过了。原因是我遇到了一个System.Web.Mvc的问题,因为我没有使用Razor,所以我试图为您手动创建标签。有趣的是这个答案在这里并不被接受,但我却从这里得到了被接受的答案...... - MichaelEvanchik

2
在新版的ActionMailer中,“RazorEngine”是一个依赖项。最新版本的RazorEngine会安装System.Web.Razor 3.0.0的依赖项。
如果您在应用程序中使用早期版本(我想您正在另一个项目中使用ActionMailer,并且您从另一个项目引用了邮件功能),那么您当然会遇到这个问题。
在早期的应用程序中,我有一个WebApplication MVC,它使用system.web.Razor版本2.0.0。 当然,我也遇到了这个问题。 如何解决? => 简单!
只需在ActionMailer项目中卸载整个ActionMailer,然后安装RazorEngine的先前版本即可。
安装-Package RazorEngine-Version 3.3.0(因为版本3.3.0将引用system.web.razor 2.0.0) 再次安装ActionMailer(它不会安装RazorEngine的最新版本,因为您已经自己安装了它)
成功!

1
你的网站应用程序的bin文件夹中是否有System.Web.WebPages.Razor.dll?它的版本是多少?(在资源管理器中右键单击,属性)我猜它不是3.0.0.0。只需获取v3.0.0.0的NuGet包,可以通过从同事那里复制packages文件夹来实现。或者手动安装:http://www.nuget.org/packages/Microsoft.AspNet.Mvc/3.0.20105.1 编辑:如果你从同事那里复制东西,请将相关版本号也复制到packages.config中。这是告诉NuGet要将哪些版本放入packages文件夹的内容。

我从PFx86/.../复制了System.Web.WebPages.Razor.dll v2.0.0.0到我的bin目录,但没有任何变化。我将删除所有的DLL和XML文件,然后复制我的同事所拥有的内容。当我尝试从NuGet更新MS.AspNet.Mvc时,出现了问题;让我再试一次并复制错误信息。 - Codes with Hammer
不要费心手动复制DLL,Visual Studio总是胜出的:)。确保你在你的packages文件夹中有正确版本的DLL(这是nuger的作用),确保该精确的DLL被引用到你的项目中(nuget也会处理它,但也许你会意外地编辑该引用),然后就完成了。 - fejesjoco
哦,MS.AN.Mvc已经更新到v5了。我会尝试打开你提供的链接。 - Codes with Hammer
嗯,又是这样。目前没有需要更新的相关软件包。但上次我检查我的项目引用的 System.Web.Razor 时,只列出了版本1和2。我知道3以前在某个地方存在过。 - Codes with Hammer
不要更新你的软件包,因为它会将MVC 3升级到5。使用包管理器控制台获取确切版本。或者像我说的那样,如果你的同事可以正常工作,只需复制他的文件夹,然后不用担心NuGet。 - fejesjoco
是的,看起来就是这样。我刚刚将MVC从5降级到3;现在我在引用MVC 4的Mvc.Mailer v1时出现了编译错误。(SecurityGuard使用了Mvc.Mailer。)MVC3也不喜欢我在密码过期属性中使用using System.Web.Mvc.Filters - Codes with Hammer

1
我不想安装Visual Studio和开发环境,所以我在Windows Server 2016机器上安装了AspNetMVC4Setup.exe,并解决了问题。该安装程序从Microsoft网站下载。

File properties of the installer


0
在我的情况下,按照上述任何方法都无法解决问题。我在包配置中拥有所有路径正确,并且所引用的dll文件已经就位,但仍然出现了System.Web.WebPages.Razor的运行时错误。我更改了本地主机端口号,这样就解决了问题 我不确定为什么会出现这个问题,以及为什么更改端口号可以解决它。只是将其发布,因为我觉得这可能对某些人有用。

0

0

我遇到了同样的错误,以下方法对我有效:

  1. 修复引用错误。
  2. 关闭Visual Studio。
  3. 删除Packages文件夹。
  4. 删除.vs文件夹。
  5. 重新运行项目。
  6. 重新构建项目。

0

我在升级到v3.0.0.preview4时遇到了这个问题,所以降级到稳定版本解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接