What is the difference between Redirect and RedirectToAction in ASP.NET MVC?

What is the difference between Redirect and RedirectToAction other than their return type? When do we use them? An explanation with any real-life scenario helped me a lot.

I looked at Confusion between Redirect and RedirectToAction , but for me it seems like a more specific answer to handling the id parameter and returning the correct view.

+43
asp.net-mvc
Aug 30 2018-12-12T00:
source share
1 answer

RedirectToAction allows you to create a redirect URL for a specific action / controller in your application, i.e. it will use a route table to create the correct URL.

Redirect requires that you provide the full URL for the redirect.

If you have an Index action on the Home controller with the Id parameter:

  • You can use RedirectToAction("Index", "Home", new { id = 5 }) , which will generate a URL for you based on your route table.

  • You can use Redirect , but you must create the URL yourself, so you pass in Redirect("/Home/Index/5") or, nevertheless, use your route table.

  • You cannot redirect to google.com (external URL) using RedirectToAction , you must use Redirect .

RedirectToAction designed to perform 302 redirects in your application and gives you an easier way to work with your route table.

Redirect designed for 302 redirects to all other, in particular external URLs, but you can still redirect in your application, you just need to create URLs yourself.

Recommendations: Use RedirectToAction for any actions with your actions / application controllers. If you use Redirect and provide a URL, you will need to manually change these URLs when the route table changes.

+69
Aug 30 '12 at 14:21
source share



All Articles