統合のためだけにプライベートな「ヘルパー」メソッドに引き出された、いくつかのコントローラーアクションに共通するコードがいくつかあります。このメソッドは、いくつかの健全性/セキュリティチェックを実行し、ユーザーを他のアクションにリダイレクトする可能性がありますが、オブジェクトを「取得」することが期待されています。
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();
}
これは、Elmahが例外を通知することを除いて、正しく機能します。
System.Web.HttpException: Cannot redirect after HTTP headers have been sent.
だから、私の質問は:私がやろうとしていることをするためのより正しい方法はありますか?基本的に、必要なのは、現在のアクションで処理を停止し、代わりにRedirectToRouteResultを返すことです。