Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-mvc

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.

like image 758
IFrizy Avatar asked Jul 07 '13 23:07

IFrizy


People also ask

What is the difference between redirect and RedirectToAction in MVC?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.

What is RedirectPermanent in asp net?

RedirectPermanent(String, Boolean) Performs a permanent redirection from the requested URL to the specified URL, and provides the option to complete the response. RedirectPermanent(String) Performs a permanent redirection from the requested URL to the specified URL.

What is redirect to action in MVC?

RedirectToAction(String, Object) Redirects to the specified action using the action name and route values. RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.

What is used to redirect from one action to another in MVC core?

Use RedirectToActionResult in ASP.NET Core MVC This action result can be used to redirect to the specified action and controller. If no controller is specified it redirects to the specified action within the current controller.

What is the difference between redirecttoaction () and redirect () in MVC?

The RedirectToAction () method makes new requests and URL in the browser's address bar is updated with the generated URL by MVC. The Redirect () method also makes new requests and URL in the browser's address bar is updated, but you have to specify the full URL to redirect.

What is the difference between redirect and redirectpermanent?

active oldest votes. 137. 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.

What is redirecttorouteresult in ASP NET?

RedirectToRouteResult ASP.NET MVC Routing creates map between URL templates with controllers and actions. This action result redirects the client to a specific route. This action takes a route name, route value and redirects us to that route with the route values provided.

How to redirect to specific URL in ASP NET webform?

The Redirect() Method. This method is used to redirect to specified URL instead of rendering HTML. In this case, the browser receives the redirect notification and make a new request for the specified URL. This also acts like Response.Redirect() in Asp.Net WebForm. In this case, you have to specify the full URL to redirect.


1 Answers

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.

like image 123
Meryovi Avatar answered Oct 02 '22 12:10

Meryovi