简体   繁体   中英

determining which server (in a web farm) the asp.net ajax request came from?

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?

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

在此处输入图片说明

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");                 
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM