根据 aspnet webapi 中的当前用户在运行时更改操作参数的类型

Changing the type of action parameter at runtime depending on current user in aspnet webapi

如何从 action filtermodel binder 中更改 TViewModel

   [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);
    }
}

或者,模型活页夹

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

}

我不会写过分夸张的评论,而是post关于我们如何使用通用控制器完成类似的事情的建议。

控制器工厂:

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);
        }
    }
}

这样注册:

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

...最后是您的控制器的外观

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);
    }
}

这是让通用控制器为 mvc 工作的最基本的示例,它可能会以某种方式实现您想要完成的目标。