web api: 添加数据到 HttpResponseMessage
web api: add data to the HttpResponseMessage
我的网站 api 中有一个操作正在返回 HttpResponseMessage
:
public async Task<HttpResponseMessage> Create([FromBody] AType payload)
{
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
await Task.Delay(1);
var t = new T { Id = 0, Name = payload.tName, Guid = Guid.NewGuid() };
var response = new MyResponse { T = t };
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ObjectContent(typeof(MyResponse), response, new JsonMediaTypeFormatter { SerializerSettings = { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore } }) };
return result;
}
现在,我的问题是,如果发出请求并且请求的 Content-Type
是 application/xml
,我应该使用 xml formatter
.
放置响应正文
有没有办法使用通用 class 并让框架根据请求的内容类型决定在运行时使用什么格式化程序?
在请求上使用 CreateResponse
扩展方法,它将允许基于关联请求进行内容协商。如果您想根据请求的内容类型强制内容类型,请从请求中获取它并将其包含在创建响应重载中。
public class MyApitController : ApiController {
[HttpPost]
public async Task<HttpResponseMessage> Create([FromBody] AType payload) {
if (payload == null) {
throw new ArgumentNullException(nameof(payload));
}
await Task.Delay(1);
var t = new T { Id = 0, Name = payload.tName, Guid = Guid.NewGuid() };
var response = new MyResponse { T = t };
var contentType = Request.Content.Headers.ContentType;
var result = Request.CreateResponse(HttpStatusCode.OK, response, contentType);
return result;
}
}
理想情况下,返回的类型应基于请求表明它想要接受的内容。该框架确实允许在该主题上具有灵活性。
查看此以获取更多信息Content Negotiation in ASP.NET Web API
一个更简单的方法是使用 Web API2 ApiController 中的便捷方法。
[HttpPost]
public async Task<IHttpActionResult> Create([FromBody] AType payload)
{
if (payload == null)
{
return BadRequest("Must provide payload");
}
await Task.Delay(1);
var t = new T { Id = 0, Name = payload.tName, Guid = Guid.NewGuid() };
var response = new MyResponse { T = t };
return Ok(response);
}
我的网站 api 中有一个操作正在返回 HttpResponseMessage
:
public async Task<HttpResponseMessage> Create([FromBody] AType payload)
{
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
await Task.Delay(1);
var t = new T { Id = 0, Name = payload.tName, Guid = Guid.NewGuid() };
var response = new MyResponse { T = t };
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ObjectContent(typeof(MyResponse), response, new JsonMediaTypeFormatter { SerializerSettings = { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore } }) };
return result;
}
现在,我的问题是,如果发出请求并且请求的 Content-Type
是 application/xml
,我应该使用 xml formatter
.
有没有办法使用通用 class 并让框架根据请求的内容类型决定在运行时使用什么格式化程序?
在请求上使用 CreateResponse
扩展方法,它将允许基于关联请求进行内容协商。如果您想根据请求的内容类型强制内容类型,请从请求中获取它并将其包含在创建响应重载中。
public class MyApitController : ApiController {
[HttpPost]
public async Task<HttpResponseMessage> Create([FromBody] AType payload) {
if (payload == null) {
throw new ArgumentNullException(nameof(payload));
}
await Task.Delay(1);
var t = new T { Id = 0, Name = payload.tName, Guid = Guid.NewGuid() };
var response = new MyResponse { T = t };
var contentType = Request.Content.Headers.ContentType;
var result = Request.CreateResponse(HttpStatusCode.OK, response, contentType);
return result;
}
}
理想情况下,返回的类型应基于请求表明它想要接受的内容。该框架确实允许在该主题上具有灵活性。
查看此以获取更多信息Content Negotiation in ASP.NET Web API
一个更简单的方法是使用 Web API2 ApiController 中的便捷方法。
[HttpPost]
public async Task<IHttpActionResult> Create([FromBody] AType payload)
{
if (payload == null)
{
return BadRequest("Must provide payload");
}
await Task.Delay(1);
var t = new T { Id = 0, Name = payload.tName, Guid = Guid.NewGuid() };
var response = new MyResponse { T = t };
return Ok(response);
}