MVC获取与发布 [英] MVC Get Vs Post

查看:63
本文介绍了MVC获取与发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究MVC概念时,我读到在'GET'动作中包含更改服务器对象状态(DB更新等)的代码不是一个好习惯. 为此提供了缓存返回数据".

While going through MVC concepts, i have read that it is not a good practice to have code inside 'GET' action which changes state of server objects( DB updates etc.,). 'Caching of return data' has been given as a reason for this.

有人可以解释一下吗?

提前谢谢!

推荐答案

浏览器通常可以将GET请求缓存在图像或脚本之类的静态数据上.但是,您也可以允许浏览器也使用[OutputCache]或其他类似方式将GET请求缓存到控制器操作中,因此,如果为GET控制器操作打开了缓存,则单击指向/Home/Index的链接可能会实际上并没有在服务器上运行Index方法,而是允许浏览器从其自己的缓存中提供页面.

Browsers can cache GET requests, generally on static data, like images or scripts. But you can also allow browsers to cache GET requests to controller actions as well, using [OutputCache] or other similar ways, so if caching is turned on for a GET controller action, it's possible that clicking on a link leading to /Home/Index doesn't actually run the Index method on the server, but rather allows the browser to serve up the page from its own cache.

使用这种思路,您可以安全地打开GET操作的缓存,在该操作中,您所提供的数据不会更改(或不经常更改),并且您知道服务器操作不会更改每次都开火.

With this line of thinking, you can safely turn on caching on GET actions in which the data you're serving up doesn't change (or doesn't change often), with the knowledge that your server action won't fire every time.

浏览器不会缓存POST,因此可以保证任何POST都可以将其发送到服务器.

POSTs won't be cached by the browser, so any POST is guaranteed to make it to the server.

这篇关于MVC获取与发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆