如何在asp.net WebForms中实现Post / Redirect / Get模式?

时间:2022-06-12 04:09:21

So I understand the basics of the prg pattern. But I haven't come across a technique for making arbitrary data available to the "get" instance of a page.

所以我理解了prg模式的基础知识。但我没有遇到过使页面的“获取”实例可以使用任意数据的技术。

For example, I might want to display different feedback messages to the user depending on their action which initiated the PostBack.

例如,我可能希望根据启动PostBack的操作向用户显示不同的反馈消息。

What I've been doing is sending an identifier as a query string parameter. This works fine but it introduces bookmarking issues and doesn't seem like it would scale very well. What if I needed to send all of the ViewState?

我一直在做的是发送一个标识符作为查询字符串参数。这工作正常,但它引入了书签问题,似乎不会很好地扩展。如果我需要发送所有ViewState怎么办?

Unfortunately, I'm tied to WebForms at the moment and haven't been able to convince my organization to migrate to mvc.

不幸的是,我现在与WebForms联系在一起并且无法说服我的组织迁移到mvc。

2 个解决方案

#1


If the fields POSTed are being persisted before the Redirect and you need to access that data after the Redirect, I would append the identifier for the data record(s) on the query string as you mention. You could also specify a status for the request (for displaying messages etc). Then on the GET page you can read out the data and do whatever with it.

如果POSTed字段在重定向之前被持久化,并且您需要在重定向之后访问该数据,我会在您提及时在查询字符串上附加数据记录的标识符。您还可以指定请求的状态(用于显示消息等)。然后在GET页面上,您可以读出数据并对其进行任何操作。

I don't see any other way to get around this as each page obtained by GET will not have access to the previous page's ViewState etc.

我没有看到任何其他方法来解决这个问题,因为GET获得的每个页面都无法访问上一页的ViewState等。

Using Server.Transfer will have the same effect as handling the POST on the original page.

使用Server.Transfer将与在原始页面上处理POST具有相同的效果。

You could use Session variables to store the POST data, but that stinks.

您可以使用会话变量来存储POST数据,但这很糟糕。

#2


I think you can still use the get method in the form element. In this case you won't be able to use the ID of your control in normal way. But you can use the Request.Params collection to get the viewstate.

我认为你仍然可以在表单元素中使用get方法。在这种情况下,您将无法以正常方式使用控件的ID。但是您可以使用Request.Params集合来获取视图状态。

Update: Sorry, I just tried again. and found that you can access your server control by its ID in codebehind. like:

更新:对不起,我刚刚再试一次。并发现您可以通过代码隐藏中的ID访问您的服务器控件。喜欢:

Response.Write(text1.Text)

see the example:

看到例子:

the aspx page(only the form element):

aspx页面(只有表单元素):

< form id="form1" runat="server" method="get">
< div>
< asp:TextBox ID="text1" runat="server" />
< asp:Button ID="button1" runat="server" OnClick="buttonClick" />
< div>
< form>

NOTE: I used a space before each" <" otherwise the code above will not be visible.

注意:我在每个“<”之前使用了一个空格,否则上面的代码将不可见。

the code behind page (only button click event):

页面后面的代码(仅按钮单击事件):

protected void buttonClick(object sender, EventArgs e)
    {
        string text = Request.Params["text1"];

        Response.Write(text);
    }

when you click the button the url will look like this:

当您单击按钮时,网址将如下所示:

http://localhost:1157/WebSite1/Default.aspx?__VIEWSTATE=%2FwEPDwUKMTkwNjc4NTIwMWRkoJEtEHZ8lHQ53QllSkz8ncpEw80%3D&__EVENTVALIDATION=%2FwEWAwKf%2BPPUCwKTjKGwCgKs34rGBvus9oxN%2FQkHlkzpKUEwrbxHLM6u&text1=ashish&button1=

#1


If the fields POSTed are being persisted before the Redirect and you need to access that data after the Redirect, I would append the identifier for the data record(s) on the query string as you mention. You could also specify a status for the request (for displaying messages etc). Then on the GET page you can read out the data and do whatever with it.

如果POSTed字段在重定向之前被持久化,并且您需要在重定向之后访问该数据,我会在您提及时在查询字符串上附加数据记录的标识符。您还可以指定请求的状态(用于显示消息等)。然后在GET页面上,您可以读出数据并对其进行任何操作。

I don't see any other way to get around this as each page obtained by GET will not have access to the previous page's ViewState etc.

我没有看到任何其他方法来解决这个问题,因为GET获得的每个页面都无法访问上一页的ViewState等。

Using Server.Transfer will have the same effect as handling the POST on the original page.

使用Server.Transfer将与在原始页面上处理POST具有相同的效果。

You could use Session variables to store the POST data, but that stinks.

您可以使用会话变量来存储POST数据,但这很糟糕。

#2


I think you can still use the get method in the form element. In this case you won't be able to use the ID of your control in normal way. But you can use the Request.Params collection to get the viewstate.

我认为你仍然可以在表单元素中使用get方法。在这种情况下,您将无法以正常方式使用控件的ID。但是您可以使用Request.Params集合来获取视图状态。

Update: Sorry, I just tried again. and found that you can access your server control by its ID in codebehind. like:

更新:对不起,我刚刚再试一次。并发现您可以通过代码隐藏中的ID访问您的服务器控件。喜欢:

Response.Write(text1.Text)

see the example:

看到例子:

the aspx page(only the form element):

aspx页面(只有表单元素):

< form id="form1" runat="server" method="get">
< div>
< asp:TextBox ID="text1" runat="server" />
< asp:Button ID="button1" runat="server" OnClick="buttonClick" />
< div>
< form>

NOTE: I used a space before each" <" otherwise the code above will not be visible.

注意:我在每个“<”之前使用了一个空格,否则上面的代码将不可见。

the code behind page (only button click event):

页面后面的代码(仅按钮单击事件):

protected void buttonClick(object sender, EventArgs e)
    {
        string text = Request.Params["text1"];

        Response.Write(text);
    }

when you click the button the url will look like this:

当您单击按钮时,网址将如下所示:

http://localhost:1157/WebSite1/Default.aspx?__VIEWSTATE=%2FwEPDwUKMTkwNjc4NTIwMWRkoJEtEHZ8lHQ53QllSkz8ncpEw80%3D&__EVENTVALIDATION=%2FwEWAwKf%2BPPUCwKTjKGwCgKs34rGBvus9oxN%2FQkHlkzpKUEwrbxHLM6u&text1=ashish&button1=