【发布时间】:2012-03-26 22:43:42
【问题描述】:
我目前正在开发一个 ASP.NET MVC 项目。
我想实现一个 ActionFilter,它负责所有权权限。用户只能访问与他在数据库中关联的实体。
现在我不想在每个控制器中实现这一点。相反,我想使用 ActionFilter。 我已经可以识别传入参数并使用以下代码读取它们的值:
控制器
[Validate(ParameterName = "userID", EntityType="User")]
public ActionMethod Edit(int userID){...
动作过滤器
public string ParameterName { get; set; }
public string EntityType { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (EntityType != null && ParameterName != null)
{
Debug.Print("Checking if user has access to the Type \"" + EntityType + "\" with the
ID " + filterContext.ActionParameters[ParameterName]);
...
到目前为止,这工作正常。但是当涉及到非原始类型(例如 User)时,我只在 filterContext.ActionParameters[ParameterName]) 中找到了一个 NULL 值;
见
[HttpPost]
[Validate(ParameterName = "user", EntityType = "User")]
public ActionResult Edit(User user)
{....
我不知道为什么。难道是因为这是一个HttpPost方法?
【问题讨论】: