title: asp.net mvc validate [HttpPost] ActionResult()
body: <p>I need to implement a ActionFilterAttribute <code>[POST]</code> ActionResult() in the controller. The problem is that I try to “redirect” to a page if validation failed... But it does not work. Validation runs, but then returns to the <code>ActionResult()</code> next line and finally when the view is returned, only then “redirected” to the page listed in the validation. Ultimately what I need is to stop the ActionResult() statements and “redirect” to the page listed in the validation. I tried <code>OnActionExecuting()</code> and <code>OnActionExecuted()</code> but does not work any</p>
  
  <p>I need to...</p>
  
  <pre><code>filterContext.HttpContext.Response.Redirect (loginUrl, true);
  </code></pre>
  
  <p>Run away, “redirecting” the page indicated</p>
  
  <p>My code:</p>
  
  <pre><code>[HelperSomeValidations("")]
  [HttpPost]
  public ActionResult Create(Pais pais)
  {
    try
    {
      PaisBLL.saveNew(pais);
    }
    catch (Exception ex) 
    {
      ViewBag.error = ex;
      return View(“Error”);
    }
    return RedirectToAction(“Index”);
  }
  
  public class HelperSomeValidations : ActionFilterAttribute
  {
  
    public HelperSomeValidations(String permiso)
    {
      this.permiso = permiso;
    }
  
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      var user = filterContext.HttpContext.Session["coco"];
      if (user == null) //validates if the user just login
      {
        //send them off to the login page
        var url = new UrlHelper(filterContext.RequestContext);
        var loginUrl = url.Content(“~/Usuario/Login”);
        filterContext.HttpContext.Response.Redirect(loginUrl, true);
      }
      else
      {
        if (permission != “”)
        {
          //does some validations with “permission”
        }
      }
   }
  </code></pre>
  
  <p>}</p>
  
  <p>Thks!</p>