ASP.NET MVC从属性重定向 [英] ASP.NET MVC redirect from attribute

查看:179
本文介绍了ASP.NET MVC从属性重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个方法属性执行重定向。它似乎工作:

I'm trying to execute a Redirect from a method attribute. It seems to work:

public class MyAttribute: ActionFilterAttribute {
    [..]
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        [..]
        filterContext.HttpContext.Response.Redirect(urlToRedirectTo, true);
        [..]

唯一的问题是,重定向它附着在方法结束后执行的,而我想重定向到prevent该方法的执行。

The only problem is that the redirect is executed after the end of the method it's attached to, while i'd like the redirect to prevent the execution of the method.

任何帮助吗?谢谢

推荐答案

您可以通过指定一个ActionResult给filterContext.Result美元的操作方法的对$ pvent执行。例如:

You can prevent execution of the action method by assigning an ActionResult to filterContext.Result. For example:

filterContext.Result = new RedirectResult(urlToRedirectTo);

这篇关于ASP.NET MVC从属性重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆