如何解决' IMvcBuilder'不包含' AddXmlDataContractSerializerFormatters'

时间:2018-01-31 16:31:56

标签: asp.net-core-2.0 asp.net-core-webapi

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

我尝试在AddXmlDataContractSerializerFormatters();文件中添加Startup.cs,但它报告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!");
    });
}

1 个答案:

答案 0 :(得分:8)

AddXmlDataContractSerializerFormatters()扩展方法位于Microsoft.AspNetCore.Mvc.Formatters.Xml nuget package。确保项目中包含此项。使用Nuget包管理器UI或从命令行:

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

此外,请确保使用以下命令导入命名空间:

using Microsoft.Extensions.DependencyInjection;