複数の関数で同じコードを実行する必要があるセットアップがあります。
[HttpGet]
public ActionResult Index()
{
// Check if cookies are disabled, and redirect to login page if so
if (cookiesDisabled(Request))
{
ModelState.AddModelError("cookie", "");
return RedirectToAction("Login");
}
// Get the models
AsdViewModel models = getAVM();
if (models == null)
{
return Logout();
}
// Return view with the models passed in, etc.
}
public ActionResult OtherPage() {
// Do cookie check so that just typing in MySite/Controller/OtherPage won't work
// Get the models
// Do some OtherPage() relevant calculations with the models
// Return view with the models passed in, etc.
}
ここでの問題は、このコードに return-out-of がたくさんあることです。ブール値の return 関数で Cookie チェックをラップするのは冗長に思えます。これは、基本的に 1 行 ( ModelState.Add...
) を保存するだけであり、内部関数呼び出しからすべてを返すことはできないため、モデルを取得する場合と同じです。これを整理するためのより良い方法はありますか、または返品をどのように処理すればよいですか?
繰り返されるコードをスキップするために Index のようなことを実行できることはわかっていreturn OtherPage()
ますが、ユーザーが現在 にいることを URL に反映させたいのですOtherPage
。