将 ASP.NET MVC 从 4 升级到 5 时出现奇怪的错误 [英] Weird Error Upgrading ASP.NET MVC from 4 to 5

查看:26
本文介绍了将 ASP.NET MVC 从 4 升级到 5 时出现奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的项目从 MVC 4 转换为 MVC 5(以及 .Net 4 到 .Net 4.5.2,这是变化的真正驱动因素.)

当我运行其中一个页面时出现此错误(我添加了空白以方便阅读)

<块引用>

[A]System.Web.WebPages.Razor.Configuration.HostSection 不能强制转换[B]System.Web.WebPages.Razor.Configuration.HostSection.

Type A 源自 'System.Web.WebPages.Razor, Version=1.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35' 在上下文中位置的默认"'C:windowsMicrosoft.NetassemblyGAC_MSILSystem.Web.WebPages.Razorv4.0_1.0.0.0__31bf3856ad364e35System.Web.WebPages.Razor.dll'.

类型 B 源自 'System.Web.WebPages.Razor, Version=3.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35' 在上下文中位置的默认"'C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NETFilesstudentportal3g2204bad2aece9b3bassemblydl3ad80387c91adbf51_fc73d101System.Web.WebPages.Razor.dll'.

当我第一次看到这个时,啊容易!没那么多:)

我已经检查了每个项目,并确保它的版本升级到 MVC 5,其中包含 System.Web.WebPages.Razor.dll 的 3.0.0.0 版本.

干净重建,仍然得到错误.没问题,我会删除缓存的临时文件.

干净重建,问题仍然存在.我回去,在每个引用它的项目的引用中手动检查 System.Web.WebPages.Razor.dll 的每个版本.我检查了我复制 dll 的文件夹以手动引用它们,它不存在.

如果我的解决方案不包含 DLL 的副本或对 DLL 的引用,并且我已手动删除了C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary"中的缓存文件夹ASP.NET

<块引用>

文件studentportal3g...

旧的坏 dll 来自哪里?我该如何解决这个错误?我如何防止它再次发生?

谢谢,

埃里克-

解决方案

Visual Studio 是一个很棒的工具,但它在升级依赖项时并不总是做出正确的选择,也不支持所有可用的选项MSBuild.每当您发现自己陷入这样的困境时,您应该手动检查并(如有必要)编辑您的 .csproj 文件以解决它.

问题不在于您的文件存在于 GAC 中或尚未由 NuGet 安装,问题很可能是您的项目文件之一仍然引用了旧版本的 System.Web.WebPages.Razor 版本 1.0.0.0,你需要找到对它的所有引用,并相应地将它们更改为 3.0.0.0.

  1. 在解决方案资源管理器中右键单击您的项目节点,然后单击卸载项目.
  2. 再次右键单击项目节点,然后单击Edit .csproj.
  3. 在文件中搜索对 System.Web.WebPages.Razor 的引用,并相应地更新版本和 HintPath(如下所示).确保您使用的 HintPath 实际上指向现有文件.
  4. 对解决方案中的所有相关项目(以及不属于解决方案的 DLL 中的任何项目)重复这些步骤.

旧参考

<Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"><Private>True</Private><HintPath>..packagesMicrosoft.AspNet.WebPages.1.0.20105.408lib
et40System.Web.WebPages.Razor.dll</HintPath></参考>

更新参考

<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"><Private>True</Private><HintPath>..packagesMicrosoft.AspNet.WebPages.3.0.0lib
et45System.Web.WebPages.Razor.dll</HintPath></参考>

您还应该检查 web.config/Views/web.config 文件,以确保它们没有引用此程序集的任何旧版本.<块引用>

注意:如果上述说明不能解决您的问题,则问题可能超出您的解决方案.很可能有一个 3rd 方库在某处引用了该文件的旧版本.如果是这样,您可以尝试获取 DLL 的更新版本.

您可能还想查看这个问题.

I'm converting my project from MVC 4 to MVC 5 (and .Net 4 to .Net 4.5.2, which is the real driver of the changes.)

When I run one of my pages I get this error (blank space added by me for easier reading)

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection.

Type A originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:windowsMicrosoft.NetassemblyGAC_MSILSystem.Web.WebPages.Razorv4.0_1.0.0.0__31bf3856ad364e35System.Web.WebPages.Razor.dll'.

Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Filesstudentportal3g2204bad2aece9b3bassemblydl3ad80387c91adbf51_fc73d101System.Web.WebPages.Razor.dll'.

When I first saw this is though, Ah easy! Not so much :)

I've gone over every project and made sure it's version is upgraded to MVC 5 which has the 3.0.0.0 version of System.Web.WebPages.Razor.dll.

Clean rebuild, still get the error. No problem , I'll delete the cached temp files.

Clean rebuild, still get the problem. I go back, manually check each version of System.Web.WebPages.Razor.dll, in the references of each project that has a reference to it. I check my folder where I copy dlls to make references to them manually, it's not there.

If my solution doesn't' contain a copy of the DLL or a reference to the DLL, and I've manually deleted the cache folders in 'C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET

Filesstudentportal3g...

Where is the old bad dll coming from? How do I fix this error? How do I prevent it happening again?

Thanks,

Eric-

解决方案

Visual Studio is a great tool, but it doesn't always make the right choices when it comes to upgrading dependencies, nor does it support every possible option available in MSBuild. Whenever you find yourself in a bind such as this you should manually review and (if necessary) edit your .csproj file in order to resolve it.

The problem isn't that your file exists in the GAC or that it has not been installed by NuGet, the issue is most likely that one of your project files still has a reference to the old version of System.Web.WebPages.Razor version 1.0.0.0, and you need to find all references to it and change them to 3.0.0.0 accordingly.

  1. Right-click on your project node in Solution Explorer and click Unload Project.
  2. Right-click the project node again and click Edit <projectName>.csproj.
  3. Search the file for references to System.Web.WebPages.Razor and update the version and the HintPath accordingly (as shown below). Make sure the HintPath you use actually points to an existing file.
  4. Repeat these steps for all dependent projects in the solution (and any that are in DLLs that are not part of the solution).

Old Reference

<Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <Private>True</Private>        
    <HintPath>..packagesMicrosoft.AspNet.WebPages.1.0.20105.408lib
et40System.Web.WebPages.Razor.dll</HintPath>
</Reference>

Updated Reference

<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <Private>True</Private>
  <HintPath>..packagesMicrosoft.AspNet.WebPages.3.0.0lib
et45System.Web.WebPages.Razor.dll</HintPath>
</Reference>

You should also go through the web.config and /Views/web.config files to ensure that they are not referencing any old versions of this assembly.

NOTE: If the above instructions don't solve your issue, the issue likely is outside of your solution. Most likely there is a 3rd party library that is referencing the old version of the file somewhere. If so, you could attempt to get an updated version of the DLL.

You may also want to check out this question.

这篇关于将 ASP.NET MVC 从 4 升级到 5 时出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆