I’m trying to set a Http Header on every page request of a website. To do this, the code to add the header (Response.AppendHeader(“p3p”, “{HeaderInfoHere}”)) is placed in the OnActionExecuted override of the base controller (so it get’s called whenever any action is called).
The problem is, seeing as the header is appended any time an action is called (all our other controllers inherit from the base controller), the header gets appended over and over depending on how many other actions (partial views etc.) are called as part of the initial request.
I can’t query the Headers collection of the Response object – we get a PlatformNotSupported exception due to IIS integrated pipeline mode being disabled on our server (something I cannot change) so I can’t check directly if this header has already been added.
Can anyone suggest a better way to append a header (once) to a GET request in MVC or a work around for this scenario?
Add data to your ControllerContext (possibly in RouteData) noting to your code ‘I’ve added this header already’ which you check before adding it. You could also try to use the Session (and before anyone chokes – TempData already uses it)
The session is accessible via controllerContext.HttpContext.Session