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

☆樱花仙子☆ 提交于 2019-12-06 07:41:39

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