私は、年次セッションに分割されたシステムに取り組んでいます。ユーザーはセッションに移動して変更し、過去のセッションを表示できます
yearId
現在のユーザーをすべてのコントローラーに渡すにはどうすればよいですか?
認証時にユーザーの Cookie を設定できると考えていました。または、ユーザーが手動でセッションを変更し、そのようなグローバル フィルターを使用して Cookie を確認したときに、
public class MyTestAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpCookie cookie = filterContext.HttpContext.Request.Cookies["myCookie"];
//do something with cookie.Value
if (cookie!=null)
{
filterContext.ActionParameters["YearId"] = cookie.Value;
}
else
{
// do something here
}
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
}
上記のフィルターを使用する方法は次のとおりです(またはグローバルフィルターとして追加します)。
[MyTestAttribute]
public ActionResult Index(int yearId)
{
//Pass the yearId down the layers
// _repo.GetData(yearId);
return View();
}
このアプローチでは、すべてのコントローラーに yearId を追加する必要があります。フィードバックをお待ちしております。