5

I have a action that just does some db work based on the parameter passed into it, then it redirects to another page.

What should the return type be then?

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
mrblah
  • 99,669
  • 140
  • 310
  • 420

2 Answers2

4

Use RedirectToRouteResult for redirecting to same controller's action :

public RedirectToRouteResult DeleteAction(long itemId)
{
    // Do stuff
    return RedirectToAction("Index");
}

Or use this to redirect to another controller's action :

public RedirectToRouteResult DeleteAction(long itemId)
{
    // Do stuff
    return 
      new RedirectToRouteResult(
         new RouteValueDictionary(
          new {controller = "Home", action = "Index", Id = itemId})
      );
}
Canavar
  • 47,715
  • 17
  • 91
  • 122
1

If it alway redirects, the return type might as well be RedirectToRouteResult or RedirectResult, depending on whether you are redirecting to an action or a URL.

See this question for a similar discussion.

Here's an example:

public RedirectToRouteResult Foo()
{
    return this.RedirectToAction("Bar");
}
Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736