从OnActionExecuting重定向到外部URL?

5

我需要从OnActionExecuting方法重定向到外部url(比如说“www.google.com”)。目前我正在使用类似于以下内容的代码:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        var redirectUrl = "www.google.com";

        try
        {
            var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();

            if (isAjaxRequest)
            {
                filterContext.HttpContext.Response.StatusCode = SessionController.CustomHttpRedirect;
                filterContext.HttpContext.Response.StatusDescription = redirectUrl;

                filterContext.Result = new JsonResult
                {
                    Data = new { Redirect = redirectUrl },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                filterContext.Result = new RedirectResult(redirectUrl, true);

            }

                return;
            }
            else
            {
                throw new LoggedOutException();
            }
        }
        catch
        {
            throw new LoggedOutException();
        }
    }
}

问题在于它没有将我重定向到“www.google.com”,而是将我重定向到“http://localhost:1234/www.google.com”(我在本地尝试)。 有没有解决这个问题的方法? 谢谢。
2个回答

5
问题很容易解决:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        var redirectUrl = "http://www.google.com";

        try
        {
            var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();

            if (isAjaxRequest)
            {
                filterContext.HttpContext.Response.StatusCode = SessionController.CustomHttpRedirect;
                filterContext.HttpContext.Response.StatusDescription = redirectUrl;

                filterContext.Result = new JsonResult
                {
                    Data = new { Redirect = redirectUrl },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                filterContext.Result = new RedirectResult(redirectUrl, true);

            }

                return;
            }
            else
            {
                throw new LoggedOutException();
            }
        }
        catch
        {
            throw new LoggedOutException();
        }
    }
}

当我为“redirectUrl”分配值时,我只需要在www之前加上http。如果您使用SSL连接并尝试从mvc重定向到另一个域,则必须这样做。


1

使用以下代码代替:

filterContext.Result = new RedirectResult("www.google.com", true);

请尝试以下操作:
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "External" , ReturnURL = "www.google.com"}));

在您的(Home)控制器中创建一个名为(External)的操作,并从那里重定向到您的外部URL:
 public class HomeController : Controller
        {
[AllowAnonymous]
         public ActionResult External(string ReturnURL){
           return Redirect(ReturnURL);
        }
    }

你不能直接在ajax响应中执行服务器端重定向。但是,你可以返回一个带有新url的JsonResult,并使用javascript执行重定向。请参见答案

即使使用这个,它仍然无法工作,因为当我重写OnActionExecuting()方法时,在if语句中测试用户是否已经通过身份验证。使用你向我展示的这些修改后,它会在那个if语句中进入一个无限循环(如果(!HttpContext.Current.User.Identity.IsAuthenticated))。 - cozmin-calin
我在“Global.asax.cs”文件中的“RegisterGlobalFilters”方法中添加了这个过滤器。 - cozmin-calin
我已经编辑了我的答案。在控制器上的(External)操作中添加[AllowAnonymous]属性。 - Ala
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接