I have some code which is common to a number of my controller actions pulled out into a private "helper" method just for consolidation. The method is expected to "get" an object for me, though it performs some sanity/security checks and could potentially redirect the user to other actions.
private Thingy GetThingy(int id)
{
var thingy = some_call_to_service_layer(id);
if( null == thingy )
Response.Redirect( anotherActionUrl );
// ... many more checks, all more complex than a null check
return thingy;
}
public ActionResult ActionOne(int id)
{
var thingy = GetThingy(id);
// do more stuff
return View();
}
// ... n more actions
public ActionResult ActionM(int id)
{
var thingy = GetThingy(id);
// do more stuff
return View();
}
This functions properly with the exception that Elmah then notifies me of an exception:
System.Web.HttpException: Cannot redirect after HTTP headers have been sent.
So, my question is: Is there a more correct way to do what I am trying to do? Essentially, all I want is to cause the current action to stop processing and instead return a RedirectToRouteResult.