我试图弄清楚ASP.NET 5 Web API如何支持内容协商.为了支持XML(除了默认的JSON支持),根据Stack Overflow的另一个答案,我需要从可选的NuGet包中添加该支持.
自那个答案以来,情况发生了一些变化,但是我下拉了Microsoft.AspNet.Mvc.Xml
包并编辑了我的Startup.cs
文件:
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc().AddMvcOptions(options => { options.InputFormatters.Add(new XmlSerializerInputFormatter()); }); }
但是,这不会编译:
"InputFormatter"类型在未引用的程序集中定义.您必须添加对程序集"Microsoft.AspNet.Mvc.Core,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = null"的引用.
以下是完整的编译错误消息:
1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,17,33,44): DNX 4.5.1 error CS0012: The type 'InputFormatter' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Mvc.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null'. 1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,45,33,78): DNX 4.5.1 error CS1503: Argument 1: cannot convert from 'Microsoft.AspNet.Mvc.Xml.XmlSerializerInputFormatter' to 'Microsoft.AspNet.Mvc.Formatters.IInputFormatter' 1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,17,33,44): DNX Core 5.0 error CS0012: The type 'InputFormatter' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Mvc.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null'. 1>C:\Users\mark\Desktop\WebApplication1\src\WebApplication1\Startup.cs(33,45,33,78): DNX Core 5.0 error CS1503: Argument 1: cannot convert from 'Microsoft.AspNet.Mvc.Xml.XmlSerializerInputFormatter' to 'Microsoft.AspNet.Mvc.Formatters.IInputFormatter'
错误消息似乎相当清楚,但该项目已包含Microsoft.AspNet.Mvc.Core
,但不完全符合所需版本:
Id Versions -- -------- Microsoft.AspNet.IISPlatformHandler {1.0.0-rc1-final} Microsoft.AspNet.Mvc {6.0.0-rc1-final} Microsoft.AspNet.Mvc.Core {6.0.0-rc1-final} Microsoft.AspNet.Mvc.Xml {6.0.0-beta5} Microsoft.AspNet.Server.Kestrel {1.0.0-rc1-final} Microsoft.AspNet.StaticFiles {1.0.0-rc1-final} Microsoft.Extensions.Configurati... {1.0.0-rc1-final} Microsoft.Extensions.Configurati... {1.0.0-rc1-final} Microsoft.Extensions.Logging {1.0.0-rc1-final} Microsoft.Extensions.Logging.Con... {1.0.0-rc1-final} Microsoft.Extensions.Logging.Debug {1.0.0-rc1-final}
在我写这篇文章时,ASP.NET 5仅在Release Candidate版本中提供,因此上述软件包是最新的软件包.
显然,Microsoft.AspNet.Mvc.Core 6.0.0.0
不可用.在一个普通的.NET项目中,我试图用绑定重定向来解决这个问题,但由于这是ASP.NET 5,这显然不再是做事的方式.
如何编译我的代码?
我相信你必须使用Microsoft.AspNet.Mvc.Formatters.Xml包.另请参阅github上的此问题.
另外一个热门提示:使用http://packagesearch.azurewebsites.net/搜索哪个NuGet包包含您要查找的类.