title: MVC using Action Filter to check for parameters in URL. stop action from executing
body: <p>I want to make the following:</p>
  
  <p>when the url doesn't have an instID, i want to redirect to the <code>"Instelling"</code> action</p>
  
  <p>in this <strong>controller</strong>, every method needs the instID. </p>
  
  <pre><code>        [RequiredParameter(parameterName="instID", controllerToSend="Instelling")]
          public ActionResult Index(int? instID) {
              //if (!instID.HasValue) {
              //    return RedirectToAction("Index", "Instelling");
              //}
  
              var facts = _db.Instellingens.First(q =&gt; q.Inst_ID == instID).FacturatieGegevens;
  
              return View(facts);
          }
  </code></pre>
  
  <p>so this is in the controller.</p>
  
  <p><strong>the actionfilter:</strong></p>
  
  <pre><code>namespace MVC2_NASTEST.Controllers {
      public class RequiredParameterAttribute : ActionFilterAttribute {
  
          public string parameterName { get; set; }
          public string actionToSend { get; set; }
          public string controllerToSend { get; set; }
  
          public override void OnActionExecuting(ActionExecutingContext filterContext) {
  
              if (parameterName != string.Empty) {
                  if (filterContext.ActionParameters.ContainsKey(parameterName) &amp;&amp; filterContext.ActionParameters[parameterName] != null) {
                      string s = "test";
                      //all is good
                  } else {
                      //de parameter ontbreekt. kijk of de controller en de action geset zijn.
                      if (actionToSend == string.Empty)
                          actionToSend = "Index";
                      if (controllerToSend == string.Empty) {
                          controllerToSend = filterContext.Controller.ToString();
                          controllerToSend = controllerToSend.Substring(controllerToSend.LastIndexOf(".") + 1);
                          controllerToSend = controllerToSend.Substring(0, controllerToSend.LastIndexOf("Controller"));
                      }
  
                      UrlHelper helper = new UrlHelper(filterContext.RequestContext);
                      string url = helper.Action(actionToSend, controllerToSend);
  
                      HttpContext.Current.Response.Redirect(url);
                      //filterContext.HttpContext.Response.Redirect(url, true);
                  }
              }
  
              base.OnActionExecuting(filterContext);
          }
  
          public override void OnActionExecuted(ActionExecutedContext filterContext) {
  
  
              base.OnActionExecuted(filterContext);
          }
  
      }
  }
  </code></pre>
  
  <p>the thing is: it does work, however, the action itself first gets executed, THEN the redirect happens. this is not what I wanted.</p>
  
  <p>Perhaps i shouldnt use actionfilters but just add a route?
  in this case, how would i redirect the route to another controller if the instID is missing?</p>