开发者

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:

  1. Page A rendered to client
  2. Client posts back to Page A
  3. Page A code behind generates a request to Page B
  4. Page A code behind redirects user to Page B
  5. 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:

  1. Page A rendered to client, with the form post action set to Page B
  2. Clients enters information and clicks submit
  3. 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&param2=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:

  1. Redirect to the page and pass the User and Session values in the querystring

  2. Store User and Session in the Session collection then redirect

  3. If strUserName and strValue originate from another postback your could use Server.Transfer to transfer control to test.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

  1. The webserver itself POSTs the values to the url. When the page runs this time the AllKeys collection will contain the values you posted.

  2. 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:

  1. Move the processing that was being carried out by test.aspx to the page that was generating the original query.
  2. Save the User and Session values the the session state.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜