Why is the Request.Form.AllKeys collection empty after a POST and Redirect?
I have an aspx page where I want to post values to a new page and then redirect to that new page. I don't get any errors and the redirection occurs but the AllKeys
collection is always empty.
Here's an example of my code:
Try
With strPost
.Append("User=" & strUserName)
.Append("&Session=" + strValue)
End With
Dim objRequest A开发者_如何学Pythons Net.HttpWebRequest = _
Net.WebRequest.Create("http://localhost:57918/testproject/test.aspx")
With objRequest
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = strPost.ToString().Length
End With
Dim objStream As IO.StreamWriter = _
New IO.StreamWriter(objRequest.GetRequestStream())
objStream.Write(strPost.ToString)
objStream.Close()
Catch ex As Exception
Debug.Print(ex.Message)
Exit Sub
End Try
Response.Redirect("http://localhost:57918/testproject/test.aspx")
I have seen a few articles similar to this problem but none of them have helped. What am I doing wrong?
Why don't you just have your main page post directly to this other page?
If the process is:
- Page A rendered to client
- Client posts back to Page A
- Page A code behind generates a request to Page B
- Page A code behind redirects user to Page B
- Page B rendered to client
Then between steps 4 and 5 you will lose all the post params. That's just how it works.
However, you could do the following:
- Page A rendered to client, with the form post action set to Page B
- Clients enters information and clicks submit
- Post values go to page B for handling.
Another path would be to have Page A perform a redirect and pass the values on the query string. For example, Response.Redirect("/PageB.aspx?param1=value¶m2=value")
If I'm correct in understanding this, you are expecting the POST
values to be available in /testproject/test.aspx
after the redirect.
Unfortunately it won't work like that. When you perform the WebRequest it's a one-shot post. A new request is created your page executes and then the request ends and all data associated with that page will be discarded.
When you redirect at the end of the example given that is a completely new GET
request to a new instance of test.aspx
. Your previous request's POST
data will never be available.
You can either:
Redirect to the page and pass the
User
andSession
values in the querystringStore
User
andSession
in theSession
collection then redirectIf
strUserName
andstrValue
originate from another postback your could useServer.Transfer
to transfer control totest.aspx
and keep the current request's Form and QueryString collections intact.
The code above will result in two requests being made to http://localhost:57918/testproject/test.aspx
The webserver itself POSTs the values to the url. When the page runs this time the
AllKeys
collection will contain the values you posted.The client's web-browser will perform a GET request against the page. Nothing will be posted. This time the keys will be blank.
In order to pass the parameters to the other page you could encode the values in the redirect URL:
Dim url as String = "http://localhost:57918/testproject/test.aspx"
url = url + "?User=" + strUserName
url = url + "&Session=" + strValue
Response.Redirect(url)
The values would then be available using the request object (e.g. Request["User"]
).
update
If you don't want to show the data to the user; then you've really only got two other options:
- Move the processing that was being carried out by
test.aspx
to the page that was generating the original query. - Save the
User
andSession
values the the session state.
精彩评论