0

action filter 또는 model binder에서 TViewModel을 변경하는 방법은 무엇입니까?aspnet webapi의 현재 사용자에 따라 런타임에 작업 매개 변수 유형을 변경하십시오.

[HasPriviliege] 
    public IHttpActionResult Get(long id) 
    { 
     var entity = AutoMapper.Mapper.Map<TViewModel, TEntity>(model); 
     repo.Update(id, entity); 
     repo.Save(); 
     return Ok(model); 
    } 

    [HasPriviliege] 
    public IHttpActionResult Edit(long id, TViewModel model) 
    { 
     var entity = AutoMapper.Mapper.Map<TViewModel, TEntity>(model); 
     repo.Update(id, entity); 
     repo.Save(); 
     return Ok(model); 
    } 

필터는

public class HasPriviliege:ActionFilterAttribute 
{ 
    public override void OnActionExecuting(HttpActionContext actionContext) 
    { 
     if(getPrivileges()=="doctor"){ 
      //the TViewModel(view model type to bind to) should be 
      // DoctorPatientViewModel should be; 
     }else{ 
      //the TViewModel(view model type to bind to) should be 
      //ExaminationPatientViewModel 
    } 
     //base.OnActionExecuting(actionContext); 
    } 
} 

또는 alternativaly, 모델 바인더

public class IPrivilegeableModelBinder: IModelBinder 
{ 
     public object BindModel(ControllerContext controllerContext, 
          ModelBindingContext bindingContext) 
     { 
    //return (hasPriviliege()?DoctorPatientViewModel:ExaminationPatientViewModel) ; 
} 

}

+0

이것이 도움이 될지 확실하지 않지만 컨트롤러 수준에서 이와 같은 작업을 수행했습니다. 우리는 요청을 검사하고 일반적인'Controller '을 새로 만들 수있는 컨트롤러 팩토리를 만들었습니다. 그러면이 작업이이 작업을 수행 할 수 있습니다. –

+0

정확하게 'T'유형을 설정합니까? – Bellash

답변

0

오버 비 대한 코멘트를 작성하기보다는, 내가 게시 해 드리겠습니다해야 내 제네릭 컨트롤러를 사용하여 이와 비슷한 것을 달성 한 방법에 대한 제안.

컨트롤러 공장 :

public class ControllerFactory : IControllerFactory 
{ 
    public IController CreateController(RequestContext requestContext, string controllerName) 
    { 
     Type controllerType = typeof(GenericController<>); 
     Type genericType = controllerType.MakeGenericType(GetPrivilegeType()); 
     ConstructorInfo ctor = genericType.GetConstructor(new Type[]{}); 
     return (IController)ctor.Invoke(new object[] { }); 
    } 

    public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) 
    { 
     ... 
     return SessionStateBehavior.ReadOnly; 
    } 

    public void ReleaseController(IController controller) 
    { 
     if (controller is IDisposable) 
     { 
      ((IDisposable)controller).Dispose(); 
     } 
    } 

    private string GetPrivilegeType() 
    { 
     if (getPrivileges() == "doctor") { 
      return typeof(DoctorPatientViewModel); 
     } else { 
      return typeof(ExaminationPatientViewModel); 
     } 
    } 
} 

이처럼 등록 : 컨트롤러가 가장 기본적인 예입니다

public class GenericController<TViewModel> // TViewModel will be the privilege type from the factory 
    where TViewModel : IPrivilege 
{ 
    [HasPriviliege] 
    public IHttpActionResult Edit(long id, TViewModel model) 
    { 
     var entity = AutoMapper.Mapper.Map<TViewModel, TEntity>(model); 
     repo.Update(id, entity); 
     repo.Save(); 
     return Ok(model); 
    } 
} 

어떻게 보이는지 마지막으로

ControllerBuilder.Current.SetControllerFactory(new ControllerFactory()); 

... 그리고 mvc에서 일하는 일반 컨트롤러를 얻으십시오.

+0

고맙습니다. 이게 도움이되었지만, 당신의 코드를 편집하여 모든 일을 할 수있게 도와 줬어. '...' 'genericType = controllerType.MakeGenericType (new Type [] {}); ConstructorInfo ctor = genericType.GetConstructor (새 형식 [] {}); ' – Bellash

+0

좋습니다. 필요한 부분 만 가져 오는 것은 맹목적인, 컷/잘라 작업이었습니다. 지금 업데이트했습니다. –

+0

WebApi를 사용하여이 작업을 수행하는 방법을 알아 냈습니까? – Bellash

관련 문제