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

2023-10-19 16:20
文章标签 模式 翻译 mvc asp net prg

本文主要是介绍[翻译] ASP.NET MVC中的PRG模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[翻译] 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:

我们首先需要两个操作,一个用于显示登录视图,另一个用于处理登录操作,如下所示:

/// /// Displays the login view (the form to enter credentials) /// /// 显示登录视图(用于输入凭证的表单) /// public ActionResult Login() { return View("Login"); } /// /// Handles form data from the login view, in other words, the form, which /// is on "login.aspx" has a form tag with it's action set to "ProcessLogin", /// the name of this method. /// /// 处理来自登录视图的表单数据,换句话说,“login.aspx”中的表单的form标签 /// 的action被设置为“ProcessLogin”——该方法的名字。 /// public ActionResult ProcessLogin(string email, string password) { IUser user = userRepository.GetByEmail(email); if (user != null && authenticator.VerifyAccount(user, password)) { authenticator.SignIn(user); return RedirectToAction("Index", "Account"); } //login failed // add some message here in TempData to tell the user the login failed // 登录失败 // 在这里向TempData中添加一些消息,告诉用户登录失败了 return RedirectToAction("Login"); }

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”属性。

Login Email Password " class="submit" />

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模式,还真对得起咱这张网页。

转载于:https://www.cnblogs.com/AndersLiu/archive/2008/09/08/prg-pattern-in-the-aspnet-mvc-framework.html

这篇关于[翻译] ASP.NET MVC中的PRG模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/240945

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

如何解决Spring MVC中响应乱码问题

《如何解决SpringMVC中响应乱码问题》:本文主要介绍如何解决SpringMVC中响应乱码问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC最新响应中乱码解决方式以前的解决办法这是比较通用的一种方法总结Spring MVC最新响应中乱码解

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

基于@RequestParam注解之Spring MVC参数绑定的利器

《基于@RequestParam注解之SpringMVC参数绑定的利器》:本文主要介绍基于@RequestParam注解之SpringMVC参数绑定的利器,具有很好的参考价值,希望对大家有所帮助... 目录@RequestParam注解:Spring MVC参数绑定的利器什么是@RequestParam?@

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Spring MVC跨域问题及解决

《SpringMVC跨域问题及解决》:本文主要介绍SpringMVC跨域问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录跨域问题不同的域同源策略解决方法1.CORS2.jsONP3.局部解决方案4.全局解决方法总结跨域问题不同的域协议、域名、端口

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服