Reputation: 24325
I know this is easy but I cant get this to work. I have a response header below.
X-Exposure-Server:EastUS2
I am spitting it out like this
<span>@Response.Headers["X-Exposure-Server"]</span>
However, it's blank. Why?
Response Headers
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Exposure-Server: EastUS2
Access-Control-Allow-Origin: *
X-UA-Compatible: IE=Edge,chrome=1
Date: Tue, 19 Apr 2016 04:39:53 GMT
Content-Length: 11017
Upvotes: 1
Views: 1387
Reputation: 24325
The only way I got this to work was set this in the BeginRequest, EndRequest was after the page was rendered.
protected void Application_BeginRequest()
{
HttpContext.Current.Response.AddHeader("X-Exposure-Server", Config.Settings.ServerRegion);
}
Upvotes: 1
Reputation: 990
The key is accessible only if its set through code.So check this approach create a class which inherits IHttpModule and implement it.
public class HTTPHeaderModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_EndRequest(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
response.AddHeader("X-Exposure-Server","EastUS2e");
//You can read this(EastUS2e) value from web.config
}
}
Now add a line in web.config in the HttpModule section:
<httpModules>
<add name="HTTPHeaderModule" , type="HTTPHeaderModule" />
</httpModules>
MSDN ref:
http://msdn.microsoft.com/en-us/library/aa719858%28VS.71%29.aspx
Upvotes: 1
Reputation: 990
If you are adding custom header to response, then it is available.Just check the values of Response.Headers in quick watch.Tried your scenario.I added my response header Response.AddHeader("myheader", "myheadervalue"); and tried to consume it in view like - @Response.Headers["myheader"],it displayed the value.Only the headers added by mvc are displayed.
Upvotes: 1