加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用Action Filter检查URL中的参数的MVC.停止执

发布时间:2020-12-16 07:22:13 所属栏目:asp.Net 来源:网络整理
导读:我想做以下事情: 当url没有instID时,我想重定向到“Instelling”动作 在这个控制器中,每个方法都需要instID. [RequiredParameter(parameterName="instID",controllerToSend="Instelling")] public ActionResult Index(int? instID) { //if (!instID.HasValu
我想做以下事情:

当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,我将如何将路由重定向到另一个控制器?

解决方法

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

像这样的东西(伪代码):

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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读