在 dotnet 6 web wpi 中发布 XML
Posting XML in dotnet 6 web wpi
builder.Services.AddControllers().AddXmlSerializerFormatters(); //I added xml
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
public class WeatherForecastList //Class I want to post in xml
{
public List<WeatherForecast> Forecasts { get; set; }
}
[HttpPost] //Experimental method
public ActionResult<WeatherForecastList> GetWeather([FromBodyAttribute]
WeatherForecastList WeatherForecast)
{
return Ok(WeatherForecast);
}
<WeatherForecastList>
<Forecasts>
<WeatherForecast>
.
.
</WeatherForecast>
</Forecasts
</WeatherForecastList>
我目前正在尝试在 dotnet 6 中使用 XML 而不是 JSON 制作 posts。如果我只是 post 一个简单的目的。我想了解如何 post 具有嵌套和其他类型列表的更复杂类型。目标是获得 xml 然后 return 它。我希望 xml 的结构看起来像上面那样。
您需要在 Program.cs
中添加 xml 格式化程序:
builder.Services.AddControllers()
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
并将 Produces
属性添加到您的控制器:
[ApiController]
[Route("[controller]")]
[Produces("application/xml")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
更新
您应该在 headers 中添加 Content-Type: application/xml
,或者如果您是从 swagger 中调用它,只需更改请求 body 在此处键入:
builder.Services.AddControllers().AddXmlSerializerFormatters(); //I added xml
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
public class WeatherForecastList //Class I want to post in xml
{
public List<WeatherForecast> Forecasts { get; set; }
}
[HttpPost] //Experimental method
public ActionResult<WeatherForecastList> GetWeather([FromBodyAttribute]
WeatherForecastList WeatherForecast)
{
return Ok(WeatherForecast);
}
<WeatherForecastList>
<Forecasts>
<WeatherForecast>
.
.
</WeatherForecast>
</Forecasts
</WeatherForecastList>
我目前正在尝试在 dotnet 6 中使用 XML 而不是 JSON 制作 posts。如果我只是 post 一个简单的目的。我想了解如何 post 具有嵌套和其他类型列表的更复杂类型。目标是获得 xml 然后 return 它。我希望 xml 的结构看起来像上面那样。
您需要在 Program.cs
中添加 xml 格式化程序:
builder.Services.AddControllers()
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
并将 Produces
属性添加到您的控制器:
[ApiController]
[Route("[controller]")]
[Produces("application/xml")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
更新
您应该在 headers 中添加 Content-Type: application/xml
,或者如果您是从 swagger 中调用它,只需更改请求 body 在此处键入: