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) ;
}
}
이것이 도움이 될지 확실하지 않지만 컨트롤러 수준에서 이와 같은 작업을 수행했습니다. 우리는 요청을 검사하고 일반적인'Controller'을 새로 만들 수있는 컨트롤러 팩토리를 만들었습니다. 그러면이 작업이이 작업을 수행 할 수 있습니다. –
정확하게 'T'유형을 설정합니까? – Bellash