本文介绍了动作过滤器属性未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有要运行的过滤器
public class ModelValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
context.Result = new NotFoundResult();
}
}
我已将其放置在我的后控制器上方
I have placed it above my post controller like this
[HttpPost]
[Route("Save")]
[ModelValidation]
public ActionResult Save(CreateUserDto Input)
{
CreateUserViewModel cuvm = new CreateUserViewModel
{
Id = Input.Id,
UserName = Input.UserName,
Email = Input.Email,
FirstName = Input.FirstName,
LastName = Input.LastName,
DateOfBirth = Input.DateOfBirth,
PhoneNumber = Input.PhoneNumber,
Password = Input.Password,
ConfirmPassword = Input.ConfirmPassword,
RoleList = Input.RoleList,
CreatedOn = Input.CreatedOn,
UpdatedOn = Input.UpdatedOn
};
//if (!ModelState.IsValid)
//{
// return BadRequest(Input);
//}
var result = applicationUsersData.Save(cuvm);
if (Input.RoleList != null)
{
var RoleList = JsonConvert.DeserializeObject<List<UserAddRoleDetailsViewModel>>(Input.RoleList);
foreach (var item in RoleList)
{
var uardvm = new SaveUserRolesDetailsViewModel
{
RoleId = item.Id,
Id = result.Id
};
userRolesData.Save(uardvm);
}
}
return Ok();
}
但是,过滤器根本不会触发.如何确保其运行?我尝试在Startup.cs中注册它,但效果不佳.
However, the filter doesn't trigger at all. How can I ensure that it runs? I have tried registering it in Startup.cs but that did not work as well.
Startup.cs
Startup.cs
services.AddAuthorization(options =>
{
options.AddPolicy(RoleGlobals.SystemAdministrator, policy => policy.Requirements.Add(new RolesFilter(RoleGlobals.SystemAdministrator, ApplicationGlobals.ApplicationName)));
});
services.AddMvc(options => {
options.Filters.Add(new ModelValidationAttribute());
});
推荐答案
请确保实现 System.Web.Mvc.ActionFilterAttribute
,而不要实现 System.Web.Http.Filters.ActionFilterAttribute
Be sure to implement System.Web.Mvc.ActionFilterAttribute
and not System.Web.Http.Filters.ActionFilterAttribute
这篇关于动作过滤器属性未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!