如何交叉检查,当我的OnActionExecuting返回True时,它是否允许我执行控制器方法,否则它会拒绝它?
public class SessionExpireAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
HttpContext ctx = HttpContext.Current;
if (2 ==2)
{
// throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.OK));
return (new HttpResponseException(new HttpResponseMessage(HttpStatusCode.OK)));
}
else
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
return;
}
base.OnActionExecuting(actionContext);
}发布于 2019-02-05 13:47:15
我认为您只需要在离开处理程序之前设置actionContext.Result属性。就像这样:
public class SessionExpireAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
// Adjust this if statement so it's executed hit if your special authentication has *failed*
if (true)
{
actionContext.Result = new HttpUnauthorizedResult("You don't have access to this resource.");
}
base.OnActionExecuting(actionContext);
}
}如果在处理程序中没有执行任何操作,则控制器操作将正常执行。
https://stackoverflow.com/questions/54535716
复制相似问题