MVC 使用 Action Filter 检查 URL 中的参数。停止执行操作

发布于 2024-09-26 12:15:55 字数 2536 浏览 3 评论 0原文

我想做以下事情:

当url没有instID时,我想重定向到

这个控制器中的“Instelling”操作,每个方法都需要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);
        }

所以这是在控制器中。

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

    }
}

事情是:它确实有效,但是,操作本身首先被执行,然后重定向发生。这不是我想要的。

也许我不应该使用actionfilters而只是添加一条路线? 在这种情况下,如果 instID 丢失,我如何将路由重定向到另一个控制器?

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲歌长辞 2024-10-03 12:15:55

您可以考虑更改为授权过滤器,而不是创建操作过滤器(在操作方法返回之前运行),它允许您重定向到替代控制器和操作过滤器。动作

是这样的(伪代码):

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" }  )
    }

}

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" }  )
    }

}

不羁少年 2024-10-03 12:15:55

这是我在 Google 上提出的问题的第一个结果,因此我想提出不同的答案。不要从操作进行重定向,而是将重定向分配给 filterContext.Result,如下所示:

filterContext.Result = new RedirectResult(url);

如果 filterContext 的 result 属性不为 null,则不会执行基础操作。由于您是在调用上下文之外执行重定向,因此您仍将执行操作方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文