LoginSignup
0
1

More than 3 years have passed since last update.

ASP.NETでAPIサーバーの返却をXMLしたい

Posted at

ASP.NETでサーバのときXMLでデータ返却したいメモ

Microsoft.AspNetCore.Mvc.Formatters.Xmlインストール

NuGetで「Microsoft.AspNetCore.Mvc.Formatters.Xml」を参照してインストール(最新版がエラーだったので2.1.1使用)
NoName_2020-2-7_12-28-9_No-00.png

Startup.csにoptons追加

RespectBrowserAcceptHeaderをtrueにしたらXMLになったのでメモ

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    services.AddMvc(options =>
    {
        // XMLを返すフォーマッターとそのMIME Typeと拡張子のマッピングを登録
        // XmlDataContractSerializerOutputFormatterはASP.NET Web API相当
        options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
        // options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
        options.FormatterMappings.SetMediaTypeMappingForFormat("xml", "application/xml");
        options.RespectBrowserAcceptHeader = true; // false by default
    });
}

ブラウザ表示

NoName_2020-2-7_12-23-47_No-00.png

参考サイト

ASP.NET MVC Core 2.0で作ったAPIでXMLなどの形式を返したい
https://www.misuzilla.org/Blog/2017/08/26/ConfigureFormattersInAspNetMvcCore2

C#(ASP.NET core)でWeb APIを作ってみる(Hello World編)
https://qiita.com/rawr/items/85abf5f646e20e3438a1#%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%83%BC%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%97%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%82%92%E5%AE%9F%E8%A3%85%E3%81%99%E3%82%8Bget%E3%81%A0%E3%81%91

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1