96

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.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Gavin
  • 17,053
  • 19
  • 64
  • 110

6 Answers6

154

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.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • 1
    If I did it this way how do I return a specific view from a controller Action. – Gavin Feb 13 '09 at 16:07
  • 11
    @Craig - the return View doesn't do a redirect and the URL ends up being the same as before. So, if you are at "/edit/MyItem" then returning View("showitem") will give you the "showitem" view with the "/edit/" url. Depending on your situation this can work, but not always. – jeffreypriebe Apr 22 '11 at 16:14
  • 5
    @jeffrey, that is two separate steps: Redirect in the first action, as John said, then return View in the second. The first action can't (directly, anyway) tell the second which view to use. – Craig Stuntz Apr 23 '11 at 21:51
  • @Craig that helps a bit. There is an "action" and a "view" - two separate things. Why then does my answer below work: RedirectToAction - no change of View. I am sure this is .net MVC 101 but I don't understand. Thanks. – jeffreypriebe Apr 25 '11 at 16:59
  • In the interests of completeness, ASP.Net MVC issues an HTTP Status Code 302 in order to cause the redirect to happen. – aboy021 Apr 03 '13 at 02:48
  • Ideally ASP.net MVC would have issued a `303 See Other`, rather than `302 Found`. [From W3C](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html): *"RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method. The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client."* – Ian Boyd Aug 19 '13 at 15:01
22
 if (true)
 {
   return View();
 }
 else
 {
   return View("another view name");
 }
Brandon
  • 68,708
  • 30
  • 194
  • 223
Mahender Reddy
  • 237
  • 2
  • 2
  • 2
    This uses the same controller Action. RedirectToAction calls a different action. This was the easiest solution for me. – RaoulRubin Mar 26 '12 at 17:46
8

The simplest way is use return View.

return View("ViewName");

Remember, the physical name of the "ViewName" should be something like ViewName.cshtml in your project, if your are using MVC C# / .NET.

Maeda
  • 1,291
  • 15
  • 16
4

Here's what you can do:

return View("another view name", anotherviewmodel);
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Andrew
  • 191
  • 1
  • 2
4

I am not 100% sure what the conditions are for this, but for me the above didn't work directly, thought it got close. I think it was because I needed "id" for my view by in the model it was called "ObjectID".

I had a model with a variety of pieces of information. I just needed the id.

Before the above I created a new System.Web.Routing.RouteValueDictionary object and added the needed id.

(System.Web.Routing.)RouteValueDictionary RouteInfo = new RouteValueDictionary();
RouteInfo.Add("id", ObjectID);
return RedirectToAction("details", RouteInfo);

(Note: the MVC project in question I didn't create, so I don't know where all the right "fiddly" bits are.)

jeffreypriebe
  • 2,267
  • 25
  • 30
0
return RedirectToAction("index");

This is how I use it in the controller and actionresult that needs to be redirected.

sa-es-ir
  • 3,722
  • 2
  • 13
  • 31
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 01 '22 at 14:24