我有以下自定义验证器:
public class CAuthorize : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
//if ajax request set status code and end responcse
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.HttpContext.Response.Write(/*some data*/);
filterContext.HttpContext.Response.End();
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
和以下动作
[HttpPost]
[CAuthorize]
public ActionResult Comment(Comment comment_obj)
{
if (ModelState.IsValid)
{
this.tblSomthing.comments.Add(comment_obj);
this.db.SaveChanges();
}
return /*some view*/
}
为什么当验证器失败时执行操作并保存数据库记录但响应正确(验证器中设置的响应)我只想在验证器失败时停止执行操作。