带有标头的Response.Redirect

问题描述 投票:0回答:6

我正在尝试设置标题并重定向到像这样的不同页面 -

Response.Headers.Add("id", "testtest");
Response.Redirect("http://www.somesite.com/somepage.aspx");

在 somepage.aspx 的

page_load
中,我正在检查标头请求 -

if (!string.IsNullOrEmpty(Request["id"]))
{
   // do something with "id"
}

但是

Request["id"
] 始终为空。如何获取新页面中标题的值?我不想使用查询字符串。

谢谢!

更新:

这里有更多细节 - 我有两个 ASP.NET v4 Web 应用程序(站点 1 和站点 2)在两台不同的计算机上运行。站点 1 只有一个 aspx 表单,并且上面只有一个按钮。单击按钮时,我会访问数据库并获取所需的值,并将其传递到站点 2。在站点 2 的 Global.asax 中,我将读取从站点 1 收到的标头信息并使用该值。

更新#2:

我能够让它工作--

 Response.Write(
                    string.Format(
                        @"<form action='{0}' id='test' method='POST'><input type='hidden' name='key' value={1} /></form>
                  <script type='text/javascript'>
                     document.getElementById('test').submit();
                  </script> ",
                        "http://www.somesite.com", "1234"));

在目标站点中,我能够使用 -

获取值
Request["key"]
asp.net response.redirect
6个回答
19
投票

HTTP 标头仅对当前响应有效。当您设置重定向时,当前响应包含您的自定义标头,但当浏览器遵循重定向位置时,这些标头将不再存在。此外,您在其他页面中使用

Request["id"]
,因此您需要将值作为查询字符串发送:

Response.Redirect("http://www.somesite.com/somepage.aspx?id=test");

4
投票

无法向 http 请求添加标头,只能通过浏览器或其他程序的用户操作或自行发送此类请求来启动标头。

因此,您可以简单地使用 .Net Framework 中的 http 客户端之一,例如

HttpClient
HttpWebRequest
,并使用您想要的任何标头发送自定义请求,而不是调用
Response.Redirect

但是,只能在 IIS 中向 http 响应消息添加自定义标头。执行重定向时仍然无法控制后续请求标头。

<configuration>
   <system.webServer>
      <httpProtocol>
         <redirectHeaders>
            <add name="X-Custom-Redirect-Header" value="MyRedirectValue" />
         </redirectHeaders>
      </httpProtocol>
   </system.webServer>
</configuration>

4
投票

不要通过 headers 传递 id,而是使用 Cookie。

Response.Cookies.Add(new HttpCookie("id", someId));
Response.Redirect("http://www.somesite.com/somepage.aspx");
 

考虑设置为 true httpCookie.HttpOnly 和 .Secure。

您可以使用以下代码获取此 ID:

Request.Cookies["id"];

1
投票

Response.Headers.Add("id", "testtest"); 没有达到预期的效果,因为您从未向客户端发送过 Response。如果您使用 Response.Redirect,则只需重定向到 url,并且 Request 对象不会与之前的 Response 参数混合。

您可以使用某种形式的AppContext/Session机制在这两个页面之间传递参数。


0
投票

你试过

Server.Transfer("http://www.somesite.com/somepage.aspx");
吗?这可能会为您提供所需的数据。如果没有,如果您不想使用会话变量,则必须将数据发布到原始页面上的表单。


0
投票

就我而言,这些解决方案都不起作用,我找到了一种方法,可以让我使用响应构造函数并使用http协议进行重定向。

new Response(null, {                                                                                                               
   status: 307,                                                                                                                          
   headers: {                                                                                                                            
      'Set-Cookie': `myCookie=432556; Path=/; HttpOnly; Secure; SameSite=Strict`,                                               
      Location: '/',                                                                                                                    
   },                                                                                                                                    
});
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.