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
  • 218,019
  • 46
  • 414
  • 706
mrblah
  • 93,893
  • 138
  • 301
  • 415

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,036
  • 17
  • 87
  • 121
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
  • 218,019
  • 46
  • 414
  • 706