ASP.NET MVCで、次のようなことをしたいと思います。
- ベースコントローラにのタイプをチェックさせ
ActionResult
ます。 - の場合
ActionResult
は、ViewResult
すべてのビューの共有データをロードします。 - 共有データが特定の基準を満たしている場合は、ログインページにリダイレクトします。
それをどのように実装しますか?
次のことを考えましたが、リダイレクトが機能しないようです(アクションがすでに実行されているためですか?)。これを回避する方法はありますか?
public abstract class BaseController : Controller
{
protected override void OnActionExecuted
(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
// If the result is a view result,
// then it loads the shared data (for use in shared view):
if (filterContext.Result is ViewResult)
LoadSharedData();
}
private void LoadSharedData()
{
// TODO: Loads the data that is common for all views.
// TODO: If the shared data fulfills some specific criteria,
// it will redirect to a login page.
Redirect("http://someurl");
}
}