在 ASP.NET 中,与在 Java 中设置请求属性等效的是什么?
我在后面的代码中有一些功能,执行后需要将请求转发到另一个页面。我想像您一样通过在 Java 中设置请求属性来传递数据(即 - 我不希望它出现在重定向响应的查询字符串中)。这可以用 ASP.NET (c#) 实现吗?
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#)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想转发请求并保留所有请求变量,则可以使用 Server.Transfer,也可以使用 Session。
You can use
Server.Transfer
if you want to forward the request and keep all of the Request variables, or you can use Session.您使用的是 ASP.NET Webforms 还是 MVC?以下内容会将您的请求重定向到新页面。您必须测试并查看它是否转发帖子数据(我不确定)。既然你提到了这一点,我认为 ASP.NET 没有像 java 那样内置“forward:”请求。我认为出于安全原因它只是有“重定向”。 (如果我错了,有人纠正我)。
在网络表单中:
尝试 Response.Redirect("mynewpage")。
在MVC中:
在完成您的操作方法 return Redirect("mynewpage")
我不知道您的用例,但将发布数据传递到不同的页面/请求通常不是一个好的做法。通常,发布的操作将负责持久性,然后向重定向页面发出 GET 请求。如果重定向视图需要访问发布的数据,它应该去持久化机制(DB)来检索它。这种方法比较安全,一般来说比较好实践。这是一个非常通用的指南,因此请根据您的需要使用它。
华泰
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
是 - 请参阅反射代码:
HttpModule 添加标头到请求
然而 - 问题是 - 你真的想使用请求标头吗?可能不会。使用它们是一种黑客行为。如果您只想传递信息,请使用 Context.Items 字典通过 Server.Transfer 在请求之间传输项目。
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.
根据您正在做的事情以及活动的地点,您还可以使用跨页面回发。
请参阅 http://msdn.microsoft.com/en-us/library/ms178139。 aspx
否则,我会选择 Server.Transfer 的 vcsjones 答案
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