Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

asp.net - Response.Redirect with headers

I'm trying to set the headers and redirecting to a different page like this -

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

And in the page_load of somepage.aspx, I'm checking the request for headers -

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

But Request["id"] is always null. How do I get the values of the header in the new page? I do not want to use query strings.

Thanks!

Update:

Here's a little more detail -- I have two ASP.NET v4 web applications (Site 1 and Site 2) running on two different machines. Site 1 has just one aspx form and it has only one button on it. On button click, I hit the database and get the value I need and should pass it on to Site 2. In the Global.asax of Site 2, I'll be reading the header information received from Site 1 and use the value.

Update #2:

I was able to get it to work --

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

In the destination site, I was able to get the value using -

Request["key"]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

HTTP headers are valid only for the current response. When you set a redirect the current response contains your custom header but when the browser follows the redirect location those headers are no longer present. Furthermore you are using Request["id"] in the other page so you need to sent the value as query string:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...