我试图弄清楚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());
});
}
然而,这并没有编译:
类型'输入格式'在未引用的程序集中定义。您必须添加对程序集的引用" 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,显然不再是处理事情的方法。
如何编写代码?
答案 0 :(得分:7)
我认为你必须使用Microsoft.AspNet.Mvc.Formatters.Xml包。另请参阅github上的this issue。
同样热门提示:使用http://packagesearch.azurewebsites.net/搜索哪个NuGet-package包含您要查找的课程。