开发者

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

开发者 https://www.devze.com 2023-01-19 06:37 出处:网络
I want to make the following: when the url doesn\'t have an instID, i want to redirect to the \"Instelling\" action

I want to make the following:

when the url doesn't have an instID, i want to redirect to the "Instelling" action

in this controller, every method needs the 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.

the 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: it does work, however, the action itself first gets executed, THEN the redirect happens开发者_JS百科. this is not what I wanted.

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?


Rather than creating an action filter (which runs just before the return of the action method), you could consider changing to an Authorization Filter which would allow you to redirect to an alternative controller & 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" }  )
    }

}


This was the first result on a question I asked on Google, so I'd like to propose a different answer. Instead of doing the redirect from the action assign a redirect to the filterContext.Result like this:

filterContext.Result = new RedirectResult(url);

If the result property of the filterContext is not null then the underlying action will not be executed. Because you're performing a redirect outside the context of the call you will still execute the action method.

0

精彩评论

暂无评论...
验证码 换一张
取 消