[Translation] PRG mode in ASP. NET MVC

Source: Internet
Author: User
[Translation] PRG mode in ASP. NET MVC

Address: http://devlicio.us/blogs/tim_barcz/archive/2008/08/22/prg-pattern-in-the-asp-net-mvc-framework.aspx

Translation: Anders Liu

Abstract: The POST operation does not directly return an HTML page, but returns a redirection command (using the HTTP 303 response code (sometimes 302) and the HTTP "Location" response header ), guide the browser to load another page with an http get request. This result page can be securely saved or reloaded as a bookmarks without unexpected side effects.

Have you ever been traveling through the "internets" and have been presented with the following?

When you surf the internet, have you ever seen the following?


As web developers we know what this means; a form was posted to the page and now you're re trying to refresh that same page. i'm not sure if there's been any significant usability study on the subject but I wocould imagine my Grandmother wouldn't know what to do here. enter the PRG pattern.

As a Web developer, we know what it means-the form has been POST to the page, but we are trying to refresh the same page. I don't know if it is of any significance to study this topic, but I can imagine that my grandmother certainly doesn't know what to do when it comes to this screen. Use the PRG mode.

What is the PRG Pattern?
What is the PRG mode?

While the PRG pattern isn't knew, there isn't much out there on it for. NET community. PRG stands for "Post/Redirect/Get", I'll let Wikipedia explain the rest:

Although the PRG mode is not a new thing, It is not emphasized much in the. NET community. PRG indicates "Post/Redirect/Get". Let Wikipedia explain the rest:

Instead of returning an HTML page directly, the POST operation returns a redirection command (using the HTTP 303 response code (sometimes 302) together with the HTTP "Location" response header ), instructing the browser to load a different page using an http get request. the result page can then safely be bookmarked or reloaded without unexpected side effects.

The POST operation does not directly return an HTML page, but returns a redirection command (using the HTTP 303 response code (sometimes 302) and the HTTP "Location" response header ), guide the browser to load another page with an http get request. This result page can be securely saved or reloaded as a bookmarks without unexpected side effects.

While this cocould be accomplished in webforms, it wocould be much more difficult since the postback model hangs on pages posting to themselves to implement button clicks and the like. the MVC Framework on the other hand makes the implementation of the PRG pattern extremely easy.

Although WebForms can also accomplish this function, it is very complicated because the postback model of the page needs to implement Button clicking and other operations by sending back itself. The MVC Framework makes the implementation of the PRG mode very simple.

But How? Can I See an Example?
How can this problem be solved? Here is an example. Why?

I'm going to use an Login function as an example. if the login attempt is successful, the user shocould be redirected to their account page, otherwise they shocould be redirected back to the login page.

I will use a Login function as an example. If the logon succeeds, the user will be redirected to his account page. Otherwise, the user will be redirected back to the logon page.

We first will need two actions, one for displaying the login view and one for processing the login attempt, which I 've provided below:

First, we need two operations: one for displaying the logon view and the other for processing logon operations, as shown below:

<Br/> // <br/> <summary> // Displays the login view (the form to enter credentials) <br/> /// <br/> // display the logon view (form used to enter the credential) <br/> // </summary> <p> public ActionResult Login () <br/> {<br/> return View ("Login "); <br/>}</p> <p> // <br/> <summary> // Handles form data from the login view, in other words, the form, which <br/> // is on "login. aspx "has a form tag with it's action set to" ProcessLogin ", <br/> // /The name of this method. <br/> /// <br/> // process the form data from the logon view. In other words, "login. the form tag <br/> // action of the form in aspx is set to "ProcessLogin" -- the name of the method. <Br/> // </summary> <p> public ActionResult ProcessLogin (string email, string password) <br/>{< br/> IUser user = userRepository. getByEmail (email); </p> <p> if (user! = Null & authenticator. verifyAccount (user, password) <br/>{< br/> authenticator. signIn (user); </p> <p> return RedirectToAction ("Index", "Account "); <br/>}</p> <p> // login failed <br/> // add some message here in TempData to tell the user the login failed </p> <p> // Logon Failed <br/> // Add some messages to TempData, tell the user that the logon failed <br/> return RedirectToAction ("Login"); <br/>}

Notice the different return types in the both of the methods. login () has one exit point, "return View (" Login ")" and ProcessLogin has two exit points both of which use RedirectToAction () call, which instead returns an objected of RedirectToRouteResult, a subclass of ViewResult. to properly perform PRG you must return a redirecting ViewResult from your action, such as RedirectToRouteResult, otherwise you'll get the dialog box pictured above.

Note that the return value types of the two methods are different. Login () has only one exit "return View (" Login ")", while ProcessLogin has two exits, both of which are called using RedirectToAction, it returns an object of the RedirectToRouteResult type -- a subclass of ViewResult. To correctly execute the PRG operation, you must return a ViewResult of the redirection class, such as RedirectToRouteResult. Otherwise, you will still see the dialog box shown in the preceding figure.

Here is the login view (login. aspx). The important part to pay attention to is the "action" attribute.

The following is the logon view (login. aspx ). Pay attention to the "action" attribute.

<Br/> <p>

By implementing the PRG pattern, I get nice clean urls that are bookmarkable. My users also get a nice site that is traversable and aren't presented with confusing security dialog messages.

After the PRG mode is implemented, my URL is cleaned and you can also add bookmarks. My users can also enjoy surfing, and those messy security dialog box messages will no longer be seen. Look! The PRG mode is really worthy of this webpage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.