Custom IActionResult method

71 views Asked by At

In a WebApi2 project I could create a custom action response a bit like this:

public class TestResult : IHttpActionResult
{
    private readonly int id;

    public TestResult (int id)
    {
        this.id = id;
    }

    public Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken)
    {
        return Task.FromResult(Execute());
    }

    private HttpResponseMessage Execute()
    {
        var response = new HttpResponseMessage();
        response.StatusCode = HttpStatusCode.Accepted; 
        response.Headers.Location = new Uri($"xxx\{id}");
        response.Content = new ObjectContent<Test>(new Test { TestNumber = id }, mediaType));
        return response;
    }
}

This sets the response content and location on the header.

How do I do this in .Net core?

The IActionResult result interface only has 1 method:
Task ExecuteResultAsync(ActionContext context)

Thanks

0

There are 0 answers