且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与ASP.NET后重定向,获取

更新时间:2022-12-09 12:55:31

通常,您将通过使用该查询字符串到信号加载/过程,记录一个aspx web窗体做到这一点。

让我们假设你有一个网页,它可以让你更新一些客户信息:

  http://www.mysite.com/customer.aspx

您将在查询字符串使用ID加载形式:

  http://www.mysite.com/customer.aspx?CustomerId=42

在codebehind你有这样的事情:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {
        INT客户ID = 0;
        如果(!string.IsNullOrEmpty(的Request.QueryString [客户编号]))
        {
            int.TryParse(的Request.QueryString [客户编号],出客户ID);
        }
        如果(客户ID == 0)
        {
            如果没有有效的客户ID QS传递//在这里处理情况
        }
        其他
        {
            //加载客户详细信息,绑定的控件等
            //确保处理的情况下使用id在QS没有发现顾客
        }
    }
}

然后在某处你的页面,你将有一个按钮,保存所做的更改。该按钮将不得不在code背后的一个onclick处理程序:

 保护无效SaveClicked(对象发件人,EventArgs的发送)
{
    //将更改保存到数据库这里    //重定向如果一切顺利
    的Response.Redirect(http://www.mysite.com/customer.aspx?CustomerId=
        + idOfSavedCustomer.ToString());
}

这应该主要是它。重定向会导致浏览器发出的重定向(...)的URL新的GET请求。这将加载页面时,如果(!的IsPostBack)将运行并与你刚才保存在previous回发的新值初始化页面。

有关这整个过程中,浏览器和服务器之间的通信将是这个样子:

 浏览器:GET http://www.mysite.com/customer.aspx?CustomerId=42
服务器:200(发回一些HTML)浏览器:POST http://www.mysite.com/customer.aspx?CustomerId=42(在请求中发送POST数据)
服务器:302(点http://www.mysite.com/customer.aspx?CustomerId=42)浏览器:GET http://www.mysite.com/customer.aspx?CustomerId=42
服务器:200(发送HTML)

在中间步骤中,服务器基本上是说:

你送我该职位的要求,我与该完成的。现在请大家一定要这个其他网页在这里......

事实上事实上referrs的URL相同的页面并不重要。


在回应一些冥想来的问题,你的子弹点列表:


  • 将如何执行POST到一个地方,是不是它的
    原来的形式?

您可以通过设置操作表单上属性做到这一点,或者你可以设置一项PostBackUrl 的按钮。


  • ViewState中的什么成为当您发布到没有一种形式
    阅读视图状态?

依赖。如果你只是发布形式不同的页面,你可以使用在<%@ previousPageType ... />指令告诉新页面,在这里的帖子是从哪里来的。这将simplyfy新页面上发布的数据工作。请参见此链接详细信息


  • ViewState中的什么时候变得重定向到真实的aspx
    Web表单?

查看状态在POST请求发送。重定向时,浏览器会加载一个新的页面,它会创建自己的viestate。


  • 是ViewState的根本与ASP.net不兼容
    后重定向-GET?

取决于你如何看待它。重定向后的新的页面将无权访问之前页面的视图状态


  • 是ASP.net带后重定向根本不相容 - 获取

没有。见上面的例子。


  • 如何(即什么code)你重定向到真实的aspx网页表单?

的Response.Redirect(URL)。这将发送到浏览器的响应,告诉它做一个新的GET请求。


  • 时(即在什么事件处理程序)你重定向到真实的aspx
    Web表单?

当你已经完成所有必需的处理POST请求的工作。


  • 有关问题提高你如何发布表单数据的问题。那里
    是暗示HTML表单不能使用 - 和所有表单数据
    必须添加到查询字符串。这是真的?如果是这样,为什么?如果不,
    为什么不呢?浏览器可以把表单数据的查询字符串?

重定向POST请求没有得到很好的支持,也许应该避免。它可以通过使用HTTP响应307来完成(有一些浏览器)在做的是,服务器有效地告诉浏览器的我不会处理您的请求后,请邮寄到这个其他页面,而不是


  • 一个相关的问题提到Server.Transfer的。使用Server.Transfer的是
    完全错误的,而决不是解决后重定向,获取问题
    (因为没有重定向)。是否正确?

Server.Transfer的(...)是什么,正在发生在服务器端。浏览器是没有意识到这一点。基本上一个页面可以使用Server.Transfer的,才能有索姆其他页面做一些处理,该网页将负责发送一个响应返回给浏览器。但是,浏览器会认为它是原来的页面回应。


  • C $ c修改有什么$在ASPX或aspx.cs文件发生支持
    PRG? presumably,最起码,在code必须改为发布
    除了地方MyPage.aspx。

没有,可以使用常规的回发。诀窍在于有一个(或几个)在页面上特定的事件处理程序(S),它在处理数据公布后,做了Repsonse.Redirect。

How can i implement the Post-Redirect-Get pattern with ASP.NET?

A button click performs some processing:

<asp:Button id="bbLaunch" OnCommand="bbLaunch_Click" />

User clicks the button, the spacecraft is launched, the web-page redisplays. If the user presses F5, they get the warning:

The solution to the problem is the Post-Redirect-Get pattern.

What is the method by which PRG can be implemented in ASP.NET?


The question centers around the problems of:

  • how can the <asp:Button> perform a POST to a place that isn't its original form?
  • what becomes of the ViewState when you post to a form that doesn't read view state?
  • what becomes of the ViewState when you redirect to the "real" aspx web form?
  • is ViewState fundamentally incompatible with ASP.net Post-Redirect-Get?
  • is ASP.net fundamentally incompatible with Post-Redirect--Get?
  • how (i.e. what code) do you redirect to the "real" aspx web form?
  • how (i.e. what url) do you redirect to the "real" aspx web form? A relation question mentions Response.Redirect(Request.RawUrl);
  • when (i.e. in what event handler) do you redirect to the "real" aspx web form?
  • the related questions raise issues of how you post form data. There is the implication that HTML forms cannot be used - and all form data must be added to the query string. Is this true? If so, why? If not, why not? Can a browser put form data in a query string?
  • a related question mentions Server.Transfer. Using Server.Transfer is completely wrong, and in no way solves the Post-Redirect-Get problem (because there is no Redirect). Correct?
  • what code change has to happen in the aspx or aspx.cs file to support PRG? Presumably, at the very least, the code must be changed to post somewhere besides MyPage.aspx.

In other words: How do you do Post-Redirect-Get in ASP.net?

Note: ASP.net (i.e. not ASP.net MVC)

See also

Typically you would do this by making an aspx web form that uses the querystring to signal which record to load/process.

Let's say you have a page that let's you update some customer information:

http://www.mysite.com/customer.aspx

You would load the form using an id in the querystring:

http://www.mysite.com/customer.aspx?CustomerId=42

In the codebehind you would have something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        int customerId = 0;
        if (!string.IsNullOrEmpty(Request.QueryString["CustomerId"]))
        {
            int.TryParse(Request.QueryString["CustomerId"], out customerId );
        }
        if (customerId == 0) 
        {
            //handle case when no valid customer id was passed in the qs here
        }
        else 
        {
            //load customer details, bind controls etc
            //make sure to handle the case when no customer was found using the id in the qs
        }
    }
}

Then somewhere in your page you would have a button that saves the changes. That button would have an OnClick handler in the code behind:

protected void SaveClicked(object sender, EventArgs e)
{
    //save changes to database here

    //Redirect if all went well
    Response.Redirect("http://www.mysite.com/customer.aspx?CustomerId=" 
        + idOfSavedCustomer.ToString());
}

That should basically be it. The redirect will cause the browser to issue a new GET request for the url in the Redirect(...). It will load the page, the if (!IsPostBack) will run and initialize the page with the new values you just saved in the previous post back.

For this whole process, the traffic between browser and server would look something like this:

Browser: GET http://www.mysite.com/customer.aspx?CustomerId=42
Server: 200 (send back some html)

Browser: POST http://www.mysite.com/customer.aspx?CustomerId=42 (post data sent in request)
Server: 302 (point to http://www.mysite.com/customer.aspx?CustomerId=42)

Browser: GET http://www.mysite.com/customer.aspx?CustomerId=42
Server: 200 (send html)

In the middle step, the server is basically saying:

"That post request you sent me, I'm done with that. Now please got to this other page here..."

The fact the url in fact referrs to the same page is not important.


Some musings in response to your bullet point list of questions:

  • how can the perform a POST to a place that isn't its original form?

You can do this by setting the action attribute on the form, or you can set the PostBackUrl on the button.

  • what becomes of the ViewState when you post to a form that doesn't read view state?

Depends. If you simply post the form to a different page, you can use the <%@ PreviousPageType .../> directive to tell the "new" page where the post came from. This will simplyfy working with the posted data on the new page. See this link for details.

  • what becomes of the ViewState when you redirect to the "real" aspx web form?

View state is sent in the post request. When redirecting, the browser will load a new page and it will create it's own viestate.

  • is ViewState fundamentally incompatible with ASP.net Post-Redirect-Get?

Depends on how you look at it. After the redirect the new page will not have access to the viewstate of the page before.

  • is ASP.net fundamentally incompatible with Post-Redirect--Get?

No. See example above.

  • how (i.e. what code) do you redirect to the "real" aspx web form?

Response.Redirect(url). This will send a response to the browser, telling it to do a new get request.

  • when (i.e. in what event handler) do you redirect to the "real" aspx web form?

When you have performed all the work necessary to process the post request.

  • the related questions raise issues of how you post form data. There is the implication that HTML forms cannot be used - and all form data must be added to the query string. Is this true? If so, why? If not, why not? Can a browser put form data in a query string?

Redirecting a post request is not well supported and should probably be avoided. It can be done (with some browser) by using the http response 307. When doing that, the server effectively tells the browser that "I will not process you post request, please post it to this other page instead".

  • a related question mentions Server.Transfer. Using Server.Transfer is completely wrong, and in no way solves the Post-Redirect-Get problem (because there is no Redirect). Correct?

Server.Transfer(...) is something that is taking place on the server side. The browser is not aware of it. Basically a page can use Server.Transfer in order to have som other page do some processing, and that page will be responsible for sending a response back to the browser. But the browser will think that it was the original page the responded.

  • what code change has to happen in the aspx or aspx.cs file to support PRG? Presumably, at the very least, the code must be changed to post somewhere besides MyPage.aspx.

No, a regular post back can be used. The trick is to have one (or a few) specific event handler(s) on the page which does a Repsonse.Redirect after processing the posted data.