4

I want to add httpHeader to my response

I thought to use this code:

private void AddCustomHeader()
         {
             if (string.IsNullOrEmpty(HttpContext.Response.Headers.Get("Access-Control-Allow-Origin")))
             {
                 // Custom header
                 HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
             }
         }

But it will always fails on localhost when ran with VS_server

This operation requires IIS integrated pipeline mode.

What will happen if I add a header that already exists?

HttpContext.Response.AddHeader("name","value");
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • what is exactly HttpContext.Response ? AFAIK it should be HttpContext.Current.Response – Antonio Bakula Feb 25 '13 at 10:33
  • instead of the whole block, have you tried : HttpContext.Current.Response.Headers.Set("Access-Control-Allow-Origin", "*"); ? – jbl Feb 25 '13 at 10:39
  • @jbl how would that help? – Elad Benda Feb 25 '13 at 11:36
  • @AntonioBakula so what is the difference? – Elad Benda Feb 25 '13 at 11:37
  • It answers your question "What will happen if I add a header that already exists?" : the already existing header will be replaced. About the exception you get, in VS ,open your web project properties, Web => check "Use Local IIS Web Server" (instead of "Use VS Development Server"). Depending on your configuration, you might need to check "Use IIS Express" – jbl Feb 25 '13 at 11:54
  • @jbl the question is what will happen when the header doesn't exist. It will throw excpetion? I couldn't find on MSDN – Elad Benda Feb 25 '13 at 12:08
  • what do you mean by "header doesn't exist" ? Custom header ? No exception will be thrown. You can set (almost) anything you want. BTW, this should be of some help for custom headers http://stackoverflow.com/a/15043027/1236044 – jbl Feb 25 '13 at 12:13
  • @jbl can you submit an answer? I want to approve yours. I have used the `Set` method – Elad Benda Feb 26 '13 at 07:56

2 Answers2

4

I would say (to sum-up comments) that, you should try to use :

 HttpContext.Current.Response.Headers.Set("Access-Control-Allow-Origin", "*");

as

  1. it will replace any existing value for the corresponding header
  2. Response.AddHeader is just there to maintain compatibility with previous versions

About the exception you get, you should try to open your web project properties, "Web" => check "Use Local IIS Web Server" (instead of "Use VS Development Server").

Depending on your configuration, you might need to check "Use IIS Express"

Also, this should be of some help for custom headers https://stackoverflow.com/a/15043027/1236044

Community
  • 1
  • 1
jbl
  • 15,179
  • 3
  • 34
  • 101
-1

Regarding the This operation requires IIS integrated pipeline mode error:

  1. Select the Application Pool in IIS Manager
  2. Click "Basic Settings..."
  3. Make sure "Integrated" mode is selected for "Managed pipeline mode" in the dialog

Source.

In regards to your second question (what will happen if you add a header that already exists) it will just add another value with the same name:

HttpContext.Response.AddHeader("name", "value1");
HttpContext.Response.AddHeader("name", "value2");

Will result in a header with a name name and a value value1, value2 (tried in ASP.NET 4.5, IIS Express, though I doubt this matters much.)

Daniel Liuzzi
  • 16,807
  • 8
  • 52
  • 57
  • @DanielLiuzzi I know how to confiugre the IIS, I was asking about the Visual-studio local server – Elad Benda Feb 25 '13 at 11:39
  • @EladBenda According to [this](http://stackoverflow.com/a/1774462/88709), `Response.Headers` isn't supported with Cassini or IIS 6. – Daniel Liuzzi Feb 25 '13 at 11:58
  • On IIS Express 8, in Application_BeginRequest if I add `HttpContext.Current.Response.AddHeader("name", "value1"); HttpContext.Current.Response.AddHeader("name", "value2");` for some reason I get two headers `name: value1` and `name: value2` instead of one `name: value1, value2` – JustAMartin May 30 '14 at 13:27