I've got an IHttpActionResult
in "old" ASP.NET, which has the following code for ExecuteAsync
:
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = _value == null ?
_request.CreateResponse(StatusCode) :
_request.CreateResponse(StatusCode, _value);
if (_uri != null)
{
var relativeUri = new Uri(_uri.PathAndQuery, UriKind.Relative);
response.Headers.Location = relativeUri;
}
return Task.FromResult(response);
}
Now, to translate this to the ASP.NET Core IActionResult
, I realize that I need to change the signature to
public async Task ExecuteResultAsync(ActionContext context)
I have two specific needs:
- If
_value
is not null, send it back as the body of the response; and - If
_uri
is not null, send it back in theLocation
header of the response.
I've found lots of similar questions, like this, which suggests manipulating the context
's Response
property - but this doesn't answer how to set the body for an object
(rather than a string), or this, which suggests using an ObjectResponse
, but it isn't clear to me how I would add the required header.
How do you do it?