我试图弄清楚ASP.NET 5 Web API如何支持内容协商.为了支持 XML(除了默认的 JSON支持), according to another answer here on 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()); }); }
但是,这不会编译:
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,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上的 this issue.另外一个热门提示:使用http://packagesearch.azurewebsites.net/搜索哪个NuGet包包含您要查找的类.