MVC using Action Filter to check parameters in URL. stop action from executing

I want to do the following:

when url does not have instID, I want to redirect to the "Instelling" action

In this controller , each method needs an instID.

  [RequiredParameter(parameterName="instID", controllerToSend="Instelling")] public ActionResult Index(int? instID) { //if (!instID.HasValue) { // return RedirectToAction("Index", "Instelling"); //} var facts = _db.Instellingens.First(q => q.Inst_ID == instID).FacturatieGegevens; return View(facts); } 

so this is in the controller.

actionfilter:

 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) && 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); } } } 

the thing is this: it really works, but the action itself is first executed, THEN the redirect occurs. This is not what I wanted.

Perhaps I should not use actionfilters, but just add a route? in this case, how would I redirect the route to another controller if instID is missing?

+4
source share
2 answers

Instead of creating an action filter (which starts just before the action method returns), you can consider changing to an authorization filter that allows you to redirect to an alternative controller and action

Something like this (pseudo code):

 public class RequiredParameterAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { // read instID from QueryString // if instId is null, return false, otherwise true } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.result = new RedirectToRouteResult( new { controller = "MyController" , action = "MyAction" } ) } 

}

+7
source

This was the first result on a question I asked on Google, so I would like to offer a different answer. Instead of redirecting from the action, assign the redirect to filterContext.Result as follows:

 filterContext.Result = new RedirectResult(url); 

If the result filterContext property is not null, the main action will not be performed. Since you are redirecting out of scope of the call, you will still be executing an action method.

+2
source

All Articles