2011-08-11 16 views
5

當我重定向像這樣爲什麼Response.Redirect與新的RedirectResult()有區別?

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
     filterContext.Result = new RedirectResult("https://mydom.com"); 
} 

所以瀏覽器重定向到http://mydom.com/httpS://mydom.com

,但如果我重定向這樣

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
     var res = filterContext.HttpContext.Response; 
     filterContext.Result = res.Redirect("https://mydom.com"); 
} 

所以瀏覽器正確地重定向到https://mydom.com

爲什麼有區別?

+0

好,你已經說的區別:d – Shaokan

+0

但爲什麼出現這種情況? – theateist

回答

2

首先,RedirectResult是一類,而HttpResponse.Redirect是一種方法。雖然前者將用戶重定向到指定的URI,但後者會將您重定向到給定的URL。查看URL和URI see here之間的區別。

希望幫助

+1

在反射器中,我看到了什麼RedirectResult'string url = UrlHelper.GenerateContentUrl(this.Url,context.HttpContext); context.Controller.TempData.Keep(); context.HttpContext.Response.Redirect(url,false); ',所以我可以看到ReidrectResult也重定向到URL而不是URI。我想念什麼? – theateist

+0

@theateist顯然,HttpResponse.Redirect使用true作爲默認值,因此會立即終止操作。而RedirectResult,如果你的代碼是它使用的,不會立即終止操作。 – Shaokan