8

I'm not sure where I'm suppose to put this in my Asp.net MVC website:

HttpContext.Current.Response.AppendHeader("P3P", "CP=\\\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\\\"");

I put it in the:

public static void RegisterRoutes(RouteCollection routes)
{
  HttpContext.Current.Response.AppendHeader("P3P", "CP=\\\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\\\"");
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
      "Default", // Route name
      "{controller}/{action}/{id}", // URL with parameters
      new { controller = "Account", action = "Logon", id = UrlParameter.Optional }
  );

}

But I get back

Response is not available in this context.

Anyone know where I am suppose to put this?

ErocM
  • 4,505
  • 24
  • 94
  • 161

3 Answers3

38

You can put it in the web.config:

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="P3P" value='CP="CAO PSA OUR"'/>

This way you do not need to put it in the code.

See this SO answer for details on what the value means.

Community
  • 1
  • 1
vtortola
  • 34,709
  • 29
  • 161
  • 263
9

Assuming you want this header on every response, something like this should do it

public class P3PHeaderAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.AppendHeader("P3P", "CP=\\\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\\\"");

    }
}

then add the filter to the global collection

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new P3PHeaderAttribute());
    }
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • this is what I was looking for but I already used the web.config change. I upvoted you, thank you! – ErocM Dec 20 '12 at 16:07
1

You should create a class that inherits ActionFilter and overrides OnResultExecuting() to add that header.

Then, add it to the global filters collection.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964