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

末鹿安然 提交于 2019-12-22 17:22:24

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/6478771/determining-which-server-in-a-web-farm-the-asp-net-ajax-request-came-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!