How to access Response in ApiController

Matias Cicero

I have the following method:

private async Task<IHttpActionResult> UseHttpCache(Func<Task<IHttpActionResult>> operation) {
      IHttpActionResult result = await operation();
      //Add HTTP Headers here...
}

But I can't manage to find any reference to a Response in the ControllerContext.

I could cast the IHttpActionResult to an HttpResponseMessage and easily set the headers there, but I'm not much of a fan of downcasting an interface to an implementation.

Is there a way I can set the response's headers in an ApiController?

peco

Not completely sure this works since I cant test it right now, but how about this.

Define your custom action result class that decorates the original result and adds the header values:

public class CachedResult : IHttpActionResult
{
    private readonly IHttpActionResult _decorated;
    private readonly TimeSpan _maxAge;

    public CachedResult(IHttpActionResult decorated, TimeSpan maxAge)
    {
        _decorated = decorated;
        _maxAge = maxAge;
    }

    public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = await _decorated.ExecuteAsync(cancellationToken);
        response.Headers.CacheControl = new CacheControlHeaderValue
        {
            Public = true,
            MaxAge = _maxAge
        };

        return response;
    }
}

And then pass in the configuration you want through the constructor. Like so:

private async Task<IHttpActionResult> UseHttpCache(Func<Task<IHttpActionResult>> operation)
{
    IHttpActionResult result = await operation();
    var maxAge = TimeSpan.FromHours(1);
    return new CachedResult(result, maxAge);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access Response in ApiController

From Dev

How can I access a property from an ActionFilterAttribute in my ApiController?

From Dev

Ajax response how to access

From Dev

Add a custom response header in ApiController

From Dev

I can not access my ApiController

From Dev

How to test apicontroller?

From Dev

How to use POST in ApiController

From Dev

OWIN ApiController access to the request body/stringified JSON

From Dev

How to Insert Data in CakePHP with APIController?

From Dev

How to access the data in this PHP XML response?

From Dev

How to access response headers using $resource in Angular?

From Dev

How to access $http response from the finally block

From Dev

How to access the contents of an error response in Volley?

From Dev

How to access response header using JSP EL?

From Dev

How to access Websocket Response Headers in Javascript?

From Dev

How to access raw JSON string of ngResource response?

From Dev

How to access a JSON Array response using JsonPath?

From Dev

How to access AJAX response variables in PHP

From Dev

How to get access the properties in this json response?

From Dev

How to get access Object from API response?

From Dev

How to access this these two array in $.ajax response?

From Dev

How to access the value of a json array in AJAX response

From Dev

How to access angular $http response custom headers?

From Dev

How to access _content from a api response in python

From Dev

How to access the response/redirect after stripe payment?

From Dev

How to access json response that have the following format

From Dev

how to access node express response object in this case?

From Dev

How to access data from API response

From Dev

How to access nested values in JSON response as an array

Related Related

  1. 1

    How to access Response in ApiController

  2. 2

    How can I access a property from an ActionFilterAttribute in my ApiController?

  3. 3

    Ajax response how to access

  4. 4

    Add a custom response header in ApiController

  5. 5

    I can not access my ApiController

  6. 6

    How to test apicontroller?

  7. 7

    How to use POST in ApiController

  8. 8

    OWIN ApiController access to the request body/stringified JSON

  9. 9

    How to Insert Data in CakePHP with APIController?

  10. 10

    How to access the data in this PHP XML response?

  11. 11

    How to access response headers using $resource in Angular?

  12. 12

    How to access $http response from the finally block

  13. 13

    How to access the contents of an error response in Volley?

  14. 14

    How to access response header using JSP EL?

  15. 15

    How to access Websocket Response Headers in Javascript?

  16. 16

    How to access raw JSON string of ngResource response?

  17. 17

    How to access a JSON Array response using JsonPath?

  18. 18

    How to access AJAX response variables in PHP

  19. 19

    How to get access the properties in this json response?

  20. 20

    How to get access Object from API response?

  21. 21

    How to access this these two array in $.ajax response?

  22. 22

    How to access the value of a json array in AJAX response

  23. 23

    How to access angular $http response custom headers?

  24. 24

    How to access _content from a api response in python

  25. 25

    How to access the response/redirect after stripe payment?

  26. 26

    How to access json response that have the following format

  27. 27

    how to access node express response object in this case?

  28. 28

    How to access data from API response

  29. 29

    How to access nested values in JSON response as an array

HotTag

Archive