开发者

What is the ASP.NET equivalent of setting a request attribute in Java?

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#)?


You can use Server.Transfer if you want to forward the request and keep all of the Request variables, or you can use Session.


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


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜