Redirect() vs RedirectPermanent() in ASP.NET MVC

asp.net Mvc

asp.net Mvc Problem Overview


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.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

The basic difference between the two is that RedirectPermanent sends the browser an HTTP 301 (Moved Permanently) status code whereas Redirect will send an HTTP 302 status code.

Use RedirectPermanent if 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.

Use Redirect if the resource may be available in the same location (URL) in the future.

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-exist page. In this case:

If the user will never be restored again, you should use RedirectPermanent so the browser can go directly to /user/does-not-exist in subsequent requests even if the URL points to /user/{userid}.

If the user may be restored in the future, you should use a regular Redirect.

Solution 2 - asp.net Mvc

RedirectPermanent is 301 and Redirect is 302 status code

Solution 3 - asp.net Mvc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIFrizyView Question on Stackoverflow
Solution 1 - asp.net MvcMeryoviView Answer on Stackoverflow
Solution 2 - asp.net Mvcdm03514View Answer on Stackoverflow
Solution 3 - asp.net MvcAbhishek SahaView Answer on Stackoverflow