【发布时间】:2014-07-16 14:01:45
【问题描述】:
添加WebAPI并在Global.asax注册后。
我们在这一行发现我们的网络应用中断:
Line 17: GlobalConfiguration.Configure(WebApiConfig.Register);
错误信息:
Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies.
The system cannot find the file specified.
经过一番检查,我发现我们在这个MVC 5.1 应用程序中使用了Json.net 6。这是否意味着我们必须降级到Json.net 4.5 才能使WebAPI 工作?
在我的.csproj 文件中,只有一个条目:
<Reference Include="Newtonsoft.Json, Version=6.0.3.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
当我在Manage NuGet Packages 中查看我的Json.NET 时,它还说我的Json.NET 是6.0.3 版。
另外,我的web.config中已经有bindingRedirect语句了。
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
但是,如果我查看 Visual Studio 中 Web 项目的 references。 Newtonsoft.Json 的路径指向C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll 但Copy Local 为假。
怎么可能?我们如何处理这种冲突?
【问题讨论】:
-
您确定代码正确且不应该是
WebApiConfig.Register(GlobalConfiguration.Configuration);? -
该注册语句来自asp.net/web-api/overview/extensibility/…。对吗?
-
按照 DavidG 的建议进行更正。同样的错误。
标签: c# json.net asp.net-web-api2 asp.net-mvc-5.1