[英]How to get redirect to a specific url from basecontroller
我有HomeController和BaseController,还有BaseController中的方法,我需要从该方法重定向到特定的URL。
这是代码:-
public class HomeController : BaseController
{
public ActionResult Index()
{
VerfiySomething();
CodeLine1.....
CodeLine2.....
CodeLineN.....
}
}
这是基本控制器-
public class BaseController : Controller
{
public void VerfiySomething()
{
if(based_on_some_check)
{
Redirect(myurl);
}
}
}
但是即使在BaseController中执行了“ Redirect(myurl)”之后,代码行1,2 ... N仍在HomeController中执行
我想要的是无需执行CodeLin1,2 ... N即可将其重定向到其他URL(而不是其他任何动作)
我将实现ActionFilterAttribute
。
请参阅: 从动作过滤器属性重定向
public class VerifySomethingAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (based_on_some_check)
{
filterContext.Result = new RedirectResult(url);
}
base.OnActionExecuting(filterContext);
}
}
用法:
[VerifySomething]
public ActionResult Index()
{
// Logic
}
您可以验证控制器的虚拟方法OnActionExecuting
:
class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext context)
{
if (somethingWrong)
{
context.Result = GetSomeUnhappyResult();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.