2010-11-24 5 views
6

모델 바인딩 중에 액세스 할 수있는 HttpContext 항목에 값을 추가하는 사용자 지정 동작 필터 특성을 만들고 싶습니다.ASP.NET MVC ActionFilterAttribute 모델 바인딩 전에 값 삽입

OnActionExecuting에 추가하려고했지만 모델 바인딩이 필터 전에 실행 된 것 같습니다.

어떻게 할 수 있습니까? 어쩌면 modelbinder에 필터 다음에 해고되고 내 필터에 의해 주입 된 값을 사용할 수있는 메소드를 재정의 할 수 있습니다. 내가 원하는 무엇

, 그것은 nvalid.net (www.nvalid.net)이며, 검증 컨텍스트 (I 유효성 검증에 사용하는 라이브러리가 컨텍스트를 지원을 주입하는 것입니다

내가 배치 할 수 있도록하고 싶습니다

속성 같은 내 actionresult 방법에

[ValidationContext("Prevalidation")] 

, 같은 내 사용자 정의 모델 바인더에서 발생하는 유효성 검사 유효성 검사를 수행 할 때 사용할 컨텍스트 알 수 있도록.

을 단순히 할 수없는 이유 그 커스텀 모델 바인더

답변

3

단순히 사용자 정의 모델 바인더를 작성하고 BindModel 방법으로 작업하지 않는 이유는 무엇입니까?

4

나는 그것을 성취 할 길을 찾았습니다. 반사에 의해

public class ModelBinder : DefaultModelBinder 
{ 
    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var actionName = controllerContext.RouteData.Values["action"] != null 
           ? controllerContext.RouteData.Values["action"].ToString() 
           : string.Empty; 

     var attribute = controllerContext.Controller.GetType().GetMethods() 
      .Where(x => x.Name == actionName) 
      .Where(x => x.GetCustomAttributes(false).Any(a => a.GetType() == typeof(CustomActionFilterAttribute))) 
      .Select(x => x.GetCustomAttributes(typeof(CustomActionFilterAttribute), false).FirstOrDefault()) 
      .FirstOrDefault() as CustomActionFilterAttribute; 

     if(attribute != null && attribute.AnyProperty) 
     { 
      // Do what you want 
     } 
    } 
} 

나는 속성을 찾아 내 ModelBinder를에서 사용할 수 있습니다

관련 문제