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.
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.
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.
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.
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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With