我在这里检查了答案。 How to pass parameters to a custom ActionFilter in ASP.NET MVC 2? 我想根据传入控制器的唯一ID来检查多个控制器中的条件。
这是actionfilter的代码。
public class AnswerCompletedAttribute : System.Web.Http.Filters.ActionFilterAttribute, IActionFilter
{
public string p { get; set; }
public void OnActionExecuted(ActionExecutedContext filterContext)
{
throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionParameters.ContainsKey(p))
{
var id = filterContext.ActionParameters[p] as Guid?;
if (Db.IsRespondentAnswerCompleted(id))
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Error" },
{ "action", "NotFound" }
});
}
}
}
}
我的代码在控制器中,
[AnswerCompleted(p = "uniqueId")]
public ActionResult Index(Guid? uniqueId)
{
//do something
return View();
}
我应该如何更改它才能使其正常工作?
答案 0 :(得分:0)
我发现它是从错误的类继承的,
它应该是system.web.mvc.actionfilterattribute
而不是System.Web.Http.Filters.ActionFilterAttribute