I have an API resource where you can create a new resource. That looks like so:-
[HttpPost("/content")]
public JsonResult PostContent([FromBody] ContentCreate content)
{
JsonResult result;
if (ModelState.IsValid) {
var newContent = _contentService.Create(content);
result = Json(newContent);
result.StatusCode = (int)HttpStatusCode.Created;
result.ContentType = ContentTypes.VENDOR_MIME_TYPE;
}
else {
var error = new ClientError(ModelState){
Code = ErrorCodes.INVALID_CONTENT,
Description = "Content is invalid"
};
result = Json(error);
result.StatusCode = (int)HttpStatusCode.BadRequest;
result.ContentType = ContentTypes.VENDOR_MIME_TYPE_ERROR;
}
return result;
}
This works fine as it satisfies my requirements to return a 201 status, and have a vendor specific MIME type for the Content-Type header. However, I want to return a Location header pointing at the location of the newly created resource. I am not clear on how to add the Location header to the response. I have read about CreatedResult which looks almost the perfect fit. However, I see no way to set the Content-Type when I use that.
So my question is how do I return JSON, with a 201 status code, with a Location header and a custom Content-Type?