将asp.net webapi的运行时版本由4.0升级到4.5.1时遇到的问题及解决
更新package
更改.net运行时的版本之后,出现了错误提示,说需要改新以下组件:
EntityFramework, EntityFramework.zh-Hans, Microsoft.AspNet.Mvc, Microsoft.AspNet.Mvc.zh-Hans, Microsoft.AspNet.Razor, Microsoft.AspNet.Razor.zh-Hans, Microsoft.AspNet.WebApi.Client, Microsoft.AspNet.WebApi.Client.zh-Hans, Microsoft.AspNet.WebApi.Core, Microsoft.AspNet.WebApi.Core.zh-Hans, Microsoft.AspNet.WebApi.WebHost, Microsoft.AspNet.WebApi.WebHost.zh-Hans, Microsoft.AspNet.WebPages, Microsoft.AspNet.WebPages.zh-Hans, Microsoft.Net.Http, Newtonsoft.Json
更新的方法为:
vs中,打开 工具 => nuget程序包管理器 => 程序包管理器控制台,然后在里面一行一行地输入以下的更新指令。
Update-Package Microsoft.AspNet.WebApi -Version 5.2.2
#汉化包
Update-Package Microsoft.AspNet.WebApi.Client.zh-Hans -Version 5.2.2
Update-Package Microsoft.AspNet.WebApi.Core.zh-Hans -Version 5.2.2
Update-Package Microsoft.AspNet.WebApi.WebHost.zh-Hans -Version 5.2.2
Update-Package EntityFramework -Version 6.0.1
Update-Package EntityFramework.zh-Hans -Version 6.0.1
Update-Package Microsoft.AspNet.Mvc -Version 5.2.2
Update-Package Microsoft.AspNet.Mvc.zh-Hans -Version 5.2.2
Update-Package Microsoft.AspNet.WebApi.HelpPage -Version 5.2.2
Update-Package Microsoft.AspNet.WebApi.OData -Version 5.2.2
Update-Package Microsoft.AspNet.WebApi.Tracing -Version 5.2.2
上面package的版本号参考了vs2013中的webapi项目模板中的版本号。以下是由项目模板生成的packages.config文件的内容:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Antlr" version="3.4.1.9004" targetFramework="net451" /> <package id="AutoMapper" version="4.0.0" targetFramework="net451" /> <package id="bootstrap" version="3.0.0" targetFramework="net451" /> <package id="jQuery" version="1.10.2" targetFramework="net451" /> <package id="log4net" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Cors" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Mvc" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Mvc.zh-Hans" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Razor" version="3.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Razor.zh-Hans" version="3.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" /> <package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.1.3" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Client.zh-Hans" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Core.zh-Hans" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Cors" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.WebHost.zh-Hans" version="5.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebPages" version="3.2.2" targetFramework="net451" /> <package id="Microsoft.AspNet.WebPages.zh-Hans" version="3.2.2" targetFramework="net451" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" /> <package id="Modernizr" version="2.6.2" targetFramework="net451" /> <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net451" /> <package id="Respond" version="1.2.0" targetFramework="net451" /> <package id="WebGrease" version="1.5.2" targetFramework="net451" /> </packages>
引入这些包的过程中,vs有可能提示要重启vs,请重启vs。
错误:“未能找到元数据文件”
更新完package之后,重新编译,有可能出现“未能找到元数据文件”。
解决的方法是找到出错的项目,然后去掉报错的引用项,然后再重新引用。
错误:预定义的类型"Microsoft.CSharp.RuntimeBinder.Binder"未定义或未导入 错误
再次编译,有可能出现错误提示“预定义的类型"Microsoft.CSharp.RuntimeBinder.Binder"未定义或未导入” ,解决的方法是:
(1)用记事本打开项目文件(后缀名为 .csproj ),找到<ItemGroup>项,可能会找到多个,选择其中一个,在里面加入
<Reference Include="Microsoft.CSharp" /> <Reference Include="System.Core" />
(2)保存项目文件,然后重新加载项目项目文件。
错误:[A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 [B]System.Web.WebPages.Razor.Configuration.HostSection。
出现这个问题的原因是因为配置文件中与razor有关的版本号不对。以下是解决方法:
(1)将web.config中的
<appSettings> <add key="webpages:Version" value="2.0.0.0" /> </appSettings>
改为:
<appSettings> <add key="webpages:Version" value="3.0.0.0" /> </appSettings>
(2)将Views文件夹下的web.config中的
<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>
改为:
<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>
参考资料: