Currently I have a method that returns a list of cities, the method I have it in the following way:
[ResponseType(typeof(List<Cities>))]
public IHttpActionResult GetSourceCities(int idPais)
{
try
{
var cities = GetCities(idPais);
return cities == null ? Ok(new List<Cities>()): Ok(cities);
}
catch (Exception ex)
{
Log.Write(ex.message);
return NotFound();
}
}
I want to implement cache so that you can access the list of cities quickly and efficiently. Now, to do it easily, change the type of response, from an IHttpActionResult to HttpResponseMessage so that you can add the cache attribute in the header.
[ResponseType(typeof(List<Cities>))]
public HttpResponseMessage GetSourceCities(int idPais)
{
try
{
var cities = GetCities(idPais);
//return cities == null ? Ok(new List<Cities>()): Ok(cities);
var httpResponseMessage = Request.CreateResponse<List<Cities>>(HttpStatusCode.OK, cities);
httpResponseMessage.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromMinutes(15)
};
return httpResponseMessage;
}
catch (Exception ex)
{
Log.Write(ex.message);
//return NotFound();
var httpResponseMessage = Request.CreateResponse(HttpStatusCode.NotFound);
return httpResponseMessage;
}
}
Now the question is: I do not want to use HttpResponseMessage, how to customize the IHttpActionResult to implement cache?