Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to disable the sending of http header data in api response.

like fallowing headers in asp.net web api 2

Cache-Control →no-cache
Connection →close
Content-Length →20
Content-Type →application/json; charset=utf-8
Date →Mon, 12 Jun 2017 10:06:04 GMT
Expires →-1
Pragma →no-cache
Server →Microsoft-IIS/8.5
X-AspNet-Version →4.0.30319


What I have tried:

i have tried adding fallowing code in global.asax
protected void Application_PreSendRequestHeaders()
       {
           Response.Headers.Remove("Server");
           Response.Headers.Remove("X-AspNet-Version");
           Response.Headers.Remove("Expires");
           Response.Headers.Remove("Cache-Control");
           Response.Headers.Remove("Connection");
           Response.Headers.Remove("Date");
           Response.Headers.Remove("Content-Type");


       }


and from iis http response header i have removed X powered by

with above procedure i am able to disable
Server
,
X-AspNet-Version
.
X powered by
headers but
I am unable to delete remaining header those are
Cache-Control →no-cache
Content-Length →7
Content-Type →application/json; charset=utf-8
Date →Sun, 25 Jun 2017 12:15:41 GMT
Expires →-1
Pragma →no-cache
.Even i Have tried with uriscan tool also.
Any help could be appreicated.
Posted
Updated 26-Jun-17 5:37am

1 solution

Simple: You don't.

Those headers are part of the HTTP grammar. If you removed them, your response would no longer be valid.

Hypertext Transfer Protocol - Wikipedia[^]
List of HTTP header fields - Wikipedia[^]

You can change the Cache-Control, Expires and Pragma headers by setting the CacheControl[^] property on the response.

The Content-Type and Content-Length headers will be set by the response formatter. They cannot be changed or removed, since that would break the response.

The Date is set automatically, and cannot be changed or removed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900