Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

different contenttype like image/jpeg with cache-control #3424


User avatar
0
vnetonline created

Hi, I would like to cache-control the different contenttype like image/jpeg. currently, i have written application server method which is always conveted into dynamic web api with contenttype application/JSON.

I would like to achieve the cache-control mechanism for some of the images i am returnig to client using below application service method. but when i try calling web api method from swagger dynamic web api interface, it is always content type of application/json with no-cache control.

my application service method is as below

public  HttpResponseMessage GetContractSignatures()
        {
            HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);

            
            try
            {
                byte[] buffer =  _attachmentImagesRepository.Get(1).Image;
                //var image = Image.FromStream(new MemoryStream(buffer));

           
                Image image = Image.FromStream(new MemoryStream(buffer));
                MemoryStream memoryStream = new MemoryStream();
                image.Save(memoryStream, ImageFormat.Jpeg);

                response.Content = new ByteArrayContent(memoryStream.ToArray());
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

                response.Headers.CacheControl = new CacheControlHeaderValue() { Public = true, MaxAge = new TimeSpan(1, 0, 0, 0) };
               
               
                return response;
            }
            catch (HttpException)
            {
                response.Content = new StringContent("HttpException", Encoding.UTF8, "image/jpeg");
                return response;
            }

        }

1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Do you use ASP.NET MVC 5.x or ASP.NET Core version ? I think you can move this into a MVC controller and add cache control like you want.

    Thanks.