asp.net-mvc ASP.NET MVC 中的 Redirect() 与 RedirectPermanent()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17517318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect() vs RedirectPermanent() in ASP.NET MVC
提问by IFrizy
Whats difference between Redirect()and RedirectPermanent(). I had read some articles, but I don't understand when we must use Redirect()and RedirectPermanent(). Can you show a pieces of example.
之间什么差异Redirect()和RedirectPermanent()。我读过一些文章,但我不明白什么时候必须使用Redirect()and RedirectPermanent()。你能举一个例子吗?
回答by Meryovi
The basic difference between the two is that RedirectPermanentsends the browser an HTTP 301(Moved Permanently) status code whereas Redirectwill send an HTTP 302status code.
两者之间的基本区别是RedirectPermanent向浏览器发送HTTP 301(永久移动)状态代码,而Redirect将发送HTTP 302状态代码。
Use RedirectPermanentif the resource has been moved permanently and will no longer be accessible in its previous location. Most browsers will cache this response and perform the redirect automatically without requesting the original resource again.
使用RedirectPermanent如果资源已被永久删除,将不再是先前的位置访问。大多数浏览器会缓存此响应并自动执行重定向,而无需再次请求原始资源。
Use Redirectif the resource may be available in the same location (URL) in the future.
Redirect如果资源将来可能在同一位置 (URL) 中可用,请使用。
Example
例子
Let's say that you have users in your system. You also have an option to delete existing users. Your website has a resource /user/{userid}that displays the details of a given user. If the user has been deleted, you must redirect to the /user/does-not-existpage. In this case:
假设您的系统中有用户。您还可以选择删除现有用户。您的网站有一个资源/user/{userid}可以显示给定用户的详细信息。如果用户已被删除,则必须重定向到该/user/does-not-exist页面。在这种情况下:
If the user will neverbe restored again, you should use RedirectPermanentso the browser can go directly to /user/does-not-existin subsequent requests even if the URL points to /user/{userid}.
如果用户永远不会再次恢复,您应该使用,RedirectPermanent以便浏览器可以/user/does-not-exist在后续请求中直接转到,即使 URL 指向/user/{userid}.
If the user may be restored in the future, you should use a regular Redirect.
如果用户将来可能会恢复,您应该使用常规的Redirect.
回答by dm03514
RedirectPermanentis 301 and Redirectis 302 status code
RedirectPermanent是 301Redirect是 302 状态码
回答by Abhishek Saha
They send different response codes to the browser. 301 is a permanent redirect, 302 a temp one. The end effect is the same, but if the client wants to index links (the most common client that does this will be search engines) then a permanent redirect tells the client to update its records to ignore the old link and start using the new one. A temp redirect tells the client that the page is redirecting for now, but not to delete the old link from its indexing database
它们向浏览器发送不同的响应代码。301 是永久重定向,302 是临时重定向。最终效果是一样的,但如果客户端想要索引链接(最常见的客户端是搜索引擎),那么永久重定向会告诉客户端更新其记录以忽略旧链接并开始使用新链接. 临时重定向告诉客户端页面现在正在重定向,但不会从其索引数据库中删除旧链接

