I have some functionality in the code behind, which after executing needs to forward the request to another page. I want to pass along data like you would by setting a request attribute in Java (i.e. - I don't want it in the query string of the redirected response). Is this possible with ASP.NET (c#)?
4 Answers
You can use Server.Transfer
if you want to forward the request and keep all of the Request variables, or you can use Session.

- 138,677
- 31
- 291
- 286
Are you using ASP.NET Webforms or MVC? The following will redirect your request to a new page. You'll have to test and see if it forwards post data (I'm not sure). Now that you mention it, I don't think ASP.NET has a built in "forward:" request like java does. I think it just has "redirect" for security reasons. (Someone correct me if I'm wrong).
In Webforms: try Response.Redirect("mynewpage").
In MVC: at the completion of your action method return Redirect("mynewpage")
I don't know your use case, but it is generally not good practice to pass post data to a different page/request. Typically the posted action will take care of persistence, and then a GET request will be issued to the redirect page. If the redirected view needs access to the posted data, it should go to the persistence mechanism (DB) to retrieve it. This method is more secure, and generally better practice. This is a very general guideline, so use it as your needs allow.
HTH

- 2,457
- 6
- 31
- 37
-
In webforms, a redirect does not preserve state or forward POST. Server.Transfer does. – Nikki9696 Mar 23 '11 at 15:40
-
Try a Server.Transfer("mynewpage") – kmehta Mar 23 '11 at 15:49
-
I'm getting a file not found exception now. Does Server.Transfer look to resolve the physical file path? If it does I can't use it because the page isn't a physical file, but a rewritten URL. – Mar 23 '11 at 15:53
-
Try to use a relative path instead of just the page name. Something like Server.Transfer("/mynewpage.aspx"). Substitue the correct relative path for "/mynewpage.aspx". – kmehta Mar 23 '11 at 16:16
-
Have you considered using the Session object to pass your data to the new page? It's cleaner IMO. – kmehta Mar 23 '11 at 16:18
-
It turns out part of the problem may be that I'm trying to do this from a UserControl and not an aspx. I'm going to accept vcsjones answer, since he was the first to reply, and then probably ask another question. – Mar 23 '11 at 16:29
Yes - See the reflection code at: HttpModule to add headers to request However - the question is - do you really want to use request headers? probably not. its a hack to use them. If you simply want to pass information, use the Context.Items dictionary to transfer your items between requests with Server.Transfer.

- 1
- 1

- 29,982
- 4
- 53
- 71
-
1Also - if you do user server.transfer, caution for this issue: http://support.microsoft.com/kb/316920 – Adam Tuliper Mar 23 '11 at 15:37
Depending on what you are doing and where your events are, you can also make use of Cross Page Postback.
See http://msdn.microsoft.com/en-us/library/ms178139.aspx
Otherwise, I'd go with vcsjones answer of Server.Transfer

- 6,260
- 1
- 28
- 23