When I redirect like this way
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectResult("https://mydom.com");
}
so the browser redirects to http://mydom.com/httpS://mydom.com
but if I redirect this way
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var res = filterContext.HttpContext.Response;
filterContext.Result = res.Redirect("https://mydom.com");
}
so the browser redirect correctly to https://mydom.com
Why there is the difference?
Response. Redirect simply sends a message (HTTP 302) down to the browser. Server. Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.
The Response. Redirect method redirects a request to a new URL and specifies the new URL while the Server. Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page. Both Response.
Return View doesn't make a new requests, it just renders the view without changing URLs in the browser's address bar. Return RedirectToAction makes a new request and the URL in the browser's address bar is updated with the generated URL by MVC.
For instance, instead of a "form" button that causes a postback and redirect, you could use a LinkButton that will behave like a hyperlink, allowing the browser to request the new page directly.
First of all, RedirectResult
is a class whereas HttpResponse.Redirect
is a method. While the former redirects the user to a specified URI the latter will redirect you to a given URL. To see the differences between URL and URI see here.
Hope that helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With