[翻譯] ASP.NET MVC中的PRG模式

來源:互聯網
上載者:User
[翻譯] ASP.NET MVC中的PRG模式

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

翻譯:Anders Liu

摘要:POST操作不是直接返回一個HTML頁面,而是返回一個重新導向命令(使用HTTP 303響應碼(有時是302)以及HTTP的“Location”回應標頭),引導瀏覽器使用HTTP GET請求載入另一個頁面。這個結果頁可以安全地作為書籤進行儲存或重新載入,而不會帶來非預期的副作用。

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

當你在internet上衝浪時,你是否見到過下面這玩意?


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

作為Web開發人員,我們知道它的意義——表單已經POST到頁面,但正在嘗試重新整理同一個頁面。我不知道研究這個主題是否有什麼重大意義,但我可以想象得到,我的奶奶遇到這個畫面時肯定不知道該怎麼辦。使用PRG模式吧。

What is the PRG Pattern?
PRG模式是什嗎?

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

儘管PRG模式不是什麼新鮮玩意,但在.NET社區強調的並不是很多。PRG表示“Post/Redirect/Get”,剩下的讓Wikipedia來解釋吧:

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.

POST操作不是直接返回一個HTML頁面,而是返回一個重新導向命令(使用HTTP 303響應碼(有時是302)以及HTTP的“Location”回應標頭),引導瀏覽器使用HTTP GET請求載入另一個頁面。這個結果頁可以安全地作為書籤進行儲存或重新載入,而不會帶來非預期的副作用。

While this could be accomplished in webforms, it would 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.

儘管WebForms也能完成該功能,但非常複雜,因為頁面的postback模型需要靠回傳自身來實現按鈕的單擊等操作。而MVC Framework使得實現PRG模式變得非常簡單。

But How? Can I See an Example?
怎麼做呢?給個例子唄?

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

我將用一個Login功能作例子。如果登入成功,使用者會被重新導向到他的帳戶頁面,否則會被重新導向回登入頁。

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

我們首先需要兩個操作,一個用於顯示登入視圖,另一個用於處理登入操作,如下所示:

<br />///<br /><summary>/// Displays the login view (the form to enter credentials)<br />///<br />/// 顯示登入視圖(用於輸入憑證的表單)<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 />/// 處理來自登入視圖的表單資料,換句話說,“login.aspx”中的表單的form標籤<br />/// 的action被設定為“ProcessLogin”——該方法的名字。<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> // 登入失敗<br /> // 在這裡向TempData中添加一些訊息,告訴使用者登入失敗了<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.

注意兩個方法的傳回值類型的不同。Login()只有一個出口“return View("Login")”,而ProcessLogin有兩個出口,這兩個出口都使用了RedirectToAction()調用,它返回的是RedirectToRouteResult類型——ViewResult的一個子類——的對象。要正確地執行PRG,你的操作必須返回一個重新導向類的ViewResult,如RedirectToRouteResult,否則你還是會看到前面圖中的對話方塊。

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

下面是登入視圖(login.aspx)。著重注意一下“action”屬性。

<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.

實現了PRG模式之後,我的URL乾淨了,也能加書籤了。我的使用者也可以衝浪沖得更爽了,那些混亂的安全對話方塊訊息再也不見了。您瞧准呵,PRG模式,還真對得起咱這張網頁。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.