如何修复 'IMvcBuilder' 不包含 'AddXmlDataContractSerializerFormatters' 的定义

How to fix 'IMvcBuilder' doesn't contain a definition for 'AddXmlDataContractSerializerFormatters'

我有最新的.Net Core v2.1.4。

我试图在 Startup.cs 文件中添加 AddXmlDataContractSerializerFormatters();,但它报告说 IMvcBuilder 不包含它的定义。

我的startup.cs:

{
    services.AddMvc().AddXmlDataContractSerializerFormatters();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();

    app.Run(async(context) =>
    {
        await context.Response.WriteAsync("MVC didn't find anything!");
    });
}

AddXmlDataContractSerializerFormatters() extension method is in the Microsoft.AspNetCore.Mvc.Formatters.Xml nuget package。确保您将此包含在您的项目中。使用 Nuget 包管理器 UI 或从命令行:

Install-Package Microsoft.AspNetCore.Mvc.Formatters.Xml

此外,确保您导入的命名空间具有:

using Microsoft.Extensions.DependencyInjection;