It is used to perform an HTTP redirect to a given url. Basically it will send the 302 status code along with the Location header in the response so that the client now issues a new HTTP request to this new location.
Usually you would use it like this instead of explicitly calling the constructor:
public ActionResult Index()
{
return Redirect("http://www.google.com");
}
As far as the difference between your two code snippets is concerned, well, it's more C# question than MVC related. In fact RedirectResult derives from ActionResult so both are valid syntaxes. Personally I prefer the first one as you could for example decide to change this redirect to return a view:
public ActionResult Index()
{
return View();
}
and if you have explicitly specified that the return type is RedirectResult
instead of ActionResult
you would now have to modify it to ViewResult
(probably not a big deal but it's an additional step you have to do).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…