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但只是添加路线? 解决方法
您可以考虑更改为允许您重定向到备用控制器的授权过滤器,而不是创建动作过滤器(在动作方法返回之前运行).行动
像这样的东西(伪代码): 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" } ) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 我什么时候应该在Web api中使用HttpReq
- 处理ASP.NET MVC中的CSS和图像
- asp.net – 最有效的方式…独特的随机字符串
- asp.net-mvc – 传递的JSON集合未被控制器拾取
- ASP.NET MVC应用程序安全性
- asp.net-mvc – 具有长不存在的URL的ASP.NET MVC 5 – (HTT
- asp.net – 如何指定现有的ClaimsIdentity的目的地?
- wcf – 我可以在实体框架4.3中使用RIA服务吗?
- asp.net-mvc – ActionResult上的自定义属性
- asp.net mvc 中 一种简单的 URL 重写
推荐文章
站长推荐
- asp.net-mvc-3 – 当两个网格位于同一页面时进行
- asp.net-mvc – ASP.NET MVC RenderAction重新呈
- asp.net – Ajax.ActionLink调用控制器两次
- asp.net-mvc – asp.net mvc publish不会覆盖csh
- dependency-injection – 从作用域服务工厂获取主
- 将.net核心连接到SSAS
- asp.net-mvc – 在DropDownList ASP.NET MVC中获
- 实体框架 – 等同于.HasOptional在实体框架核心1
- asp.net中的带宽监控
- asp.net-mvc – 将JSON对象作为参数传递给MVC控制
热点阅读