간단한 조치 필터를 사용하여 사용자가 로그인했는지 확인하고 사용자 ID를 검색합니다.ActionFilter에서 C# MVC의 Controller Action으로 변수를 전달하는 방법은 무엇입니까?
public class LoginFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Authenticate (somehow) and retrieve the ID
int id = Authentication.SomeMethod();
// Pass the ID through to the controller?
.....
}
}
어떻게이 ID를 내 컨트롤러 동작에 전달할 수 있습니까?
[LoginFilter]
public class Dashboard : Controller
{
public ActionResult Index()
{
// I'd like to be able to use the ID from the LoginFilter here
int id = ....
}
}
내가 할 수있는 ViewBag에 해당하는 항목이 있습니까? 아니면 필터와 컨트롤러 액션 사이에 변수와 객체를 전달할 수있는 다른 기술이 있습니까?
감사합니다. ViewBag 대신에 @Luiso의 대답에 의해 제안 된 것처럼 TempData를 사용 했음에도 불구하고 ViewData를 사용하면 예제를 사용하여 실제로 사용하는 방법에 대한 최상의 정보를 얻을 수 있으며 기술은 ViewBag에서 TempData와 동일합니다. 나는 이것이 View를위한 것이 아닌 Temporary Data라는 것을 알기 때문에 TempData를 사용하는 것이 더 논리적이다. –