Is it possible to redirect to a different view from a controller?
For example, all my controllers inherit from a custom controller that has a constructor that I want to redirect to different view if certain criteria is not met. Hope that makes sense.
To redirect the user to another action method from the controller action method, we can use RedirectToAction method. Above action method will simply redirect the user to Create action method.
You can redirect to an external URL by using Redirect Method() or via Json Result. Asp.net MVC redirect to URL: You can do URL redirect in mvc via Controller's Redirect() method. The following example, I have given redirection to google page.
RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route.
The RedirectToAction() MethodThis method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response. Redirect() in ASP.NET WebForm.
You can use the RedirectToAction()
method, then the action you redirect to can return a View. The easiest way to do this is:
return RedirectToAction("Index", model);
Then in your Index method, return the view you want.
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