开发者

ASP.NET MVC redirect from attribute

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

public class MyAttribute: ActionFilterAttribute {
 开发者_Go百科   [..]
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        [..]
        filterContext.HttpContext.Response.Redirect(urlToRedirectTo, true);
        [..]

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.

Any help? Thanks


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

filterContext.Result = new RedirectResult(urlToRedirectTo);


You need to add

filterContext.HttpContext.Response.End();

EDIT: The top is wrong the bottom is right

this should work

filterContext.Result = new RedirectResult(UrlToRedirectTo);

if you return an ActionResult then it gets executed instaed of the action itself so this should skip the action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜