确定 asp.net ajax 请求来自哪个服务器(在网络场中)?

发布于 2024-11-17 07:06:10 字数 342 浏览 5 评论 0原文

我在想我们如何找出该页面是从哪台服务器提供的:我会通过在页面上放置一个隐藏变量来实现这一点,该变量具有处理该页面的服务器的 IP 或服务器名称。但是我该如何处理 asp.net ajax 请求:那些作为部分回发发生的请求?我必须将隐藏变量放在更新面板中,但是如果页面中有很多更新面板怎么办?

我查看了另一篇SO帖子,但解决方案适用于 iis 7。iis6 的等效项是什么?我们如何读取标题?去哪里看?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-11-24 07:06:10

您可以通过 IIS MMC 设置 IIS6 自定义标头,方法是打开站点的属性,然后单击“HTTP 标头”选项卡:

在此处输入图像描述

您还可以使用 adsutil(位于 c:\InetPub\AdminScripts 中):

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

上面的命令将为默认网站配置 HTTP 标头。

使用 adsutil 时要小心,因为这会覆盖任何已配置的现有标头。

要设置多个标头,请执行以下操作:

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

更新:

关于访问客户端上的响应标头,如果您使用 ASP.NET AJAX 更新面板,请将此脚本添加到页面末尾

<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>

:将挂钩到页面请求管理器,这样当 Ajax 请求完成时,您还可以获得底层 XMLHttpRequest 对象的可见性,该对象具有响应标头的副本。

你可以用 jQuery 做类似的事情:

$.ajax({
  url: "/Home/HeadTest",
  success: function (data, textStatus, xhr) {
    var header = xhr.getResponseHeader("MyHeader");                 
  }
});

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");                 
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文