views:

63

answers:

2

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?

+1  A: 

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");
}
Mark Seemann
+3  A: 

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