2

I was thinking how we can find out which server the page was served from: I'd do so by doing something like put a hidden variable on the page which has the IP or server name from the server it got processed. But what do I do for asp.net ajax requests: those that happen as a partial postback? I'd have to put the hidden variable in the update panel, but what if there are many update panels in the page?

I checked out another SO post, but the solution was for iis 7. What is the equivalent for iis6? And how can we read the header? Where to look?

Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • What the problem - add one UpdatePanel with hiddenfield inside it and set it's UpdateMode value "Always" – Yuriy Rozhovetskiy Jun 25 '11 at 17:12
  • If there were more than one update panel, then this wouldn't be an elegant solution, because I'd have to add a hidden field in all the update panels. Then again I'd register a script block to do the job, but that doesn't seem elegant. – deostroll Jun 25 '11 at 18:28
  • The content of an UpdatePanel control is updated in the following circumstances: If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls that are inside other UpdatePanel controls and postbacks from controls that are not inside UpdatePanel controls. http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode.aspx – Yuriy Rozhovetskiy Jun 25 '11 at 19:24

1 Answers1

6

You can set IIS6 custom headers via IIS MMC by opening a site's properties then clicking on the HTTP Headers tab:

enter image description here

You can also use adsutil (found in c:\InetPub\AdminScripts):

cscript adsutil set w3svc/1/root/HttpCustomHeaders "X-Served-By:Server-001"

The command above will configure the HTTP Headers for the default website.

Be careful when using adsutil as this will overwrite any existing headers already configured.

To set multiple headers do:

cscript adsutil set w3svc/1/root/HttpCustomHeaders "X-Served-By:Server-001" "X-Powered-By:ASP.NET"

Update:

With regard to accessing the response headers on the client, if you're using an ASP.NET AJAX update panel then add this script to the end of your page:

<script type="text/javascript" language="javascript">
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endPageRequest);

  function endPageRequest(sender, args) {

    var allHeaders = args._response._xmlHttpRequest.getAllResponseHeaders();
    var headers = allHeaders.split('\n');

    // At this point you have a string array of response headers.

    // Or you can get an individual header:
    var header = args._response._xmlHttpRequest.getResponseHeader("MyHeader");

  }
</script>

This will hook into the page request manager such that when the Ajax request completes you also get visibility of the underlying XMLHttpRequest object which has a copy of the response headers.

You can do something similar with jQuery:

$.ajax({
  url: "/Home/HeadTest",
  success: function (data, textStatus, xhr) {
    var header = xhr.getResponseHeader("MyHeader");                 
  }
});
Kev
  • 118,037
  • 53
  • 300
  • 385
  • @deostroll - `cscript adsutil get w3svc/1/root/HttpCustomHeaders` – Kev Jun 26 '11 at 18:43
  • no i mean when response is sent to client from the server, the only way (from the client) to find/read the header as per my knowledge is to use tools like fiddler, or is there another way? – deostroll Jun 26 '11 at 19:05
  • @Kev, I like your answer. But its hard to do things the javascript way. The end users mainly browse the website using a special software called kioware, which is not a normal browser! Even otherwise (i.e. while using a normal browser), what is the best way I can read the value? In a normal postback (wherein the server name is written to a hidden variable) all I have to do is view the page source. If I had implemented your solution, how do I carry out my diagnosis? – deostroll Jun 30 '11 at 06:19
  • @deostroll - for the ajax stuff above you could inject the value into a hidden div. In modern browsers such as chrome you'd do Inspect Element (not view source) on the page to view the live DOM, then find this element. – Kev Jun 30 '11 at 06:51