1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Mvc.Formatters; namespace MvcOutputXml { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(config => { config.OutputFormatters.Add(new XmlSerializerOutputFormatter()); }); } public void Configure(IApplicationBuilder app) { app.UseMvc(); } } public class Greeting { public bool Hello { get; set; } public bool World { get; set; } } public class HomeController : Controller { [HttpGet("")] public IActionResult Index() { var h = new Greeting { Hello = true, World = true }; Response.ContentType = "text/xml"; return Ok(h); } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <Greeting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Hello>true</Hello> <World>true</World> </Greeting> |