In ASP.NET MVC, the RedirectResult
class is used to redirect the user's browser to a new URL. Here's an example of how to use RedirectResult
in a controller action method:
csharppublic ActionResult MyAction()
{
// Do some work
// Redirect the user's browser to a new URL
return Redirect("http://www.example.com/newurl");
}
In this example, the MyAction
method does some work and then returns a RedirectResult
using the Redirect
method. The Redirect
method takes a URL as a parameter and returns a RedirectResult
, which tells the MVC framework to send a "302 Found" status code to the user's browser and redirect the browser to the specified URL.
You can also use the RedirectToAction
method to redirect the user to another action method in the same or a different controller. Here's an example:
csharppublic ActionResult MyAction()
{
// Do some work
// Redirect the user to the "Index" action method in the "Home" controller
return RedirectToAction("Index", "Home");
}
In this example, the MyAction
method redirects the user to the Index
action method in the Home
controller. The RedirectToAction
method takes two parameters: the name of the action method to redirect to, and the name of the controller that contains the action method. The method returns a RedirectResult
that tells the MVC framework to redirect the user's browser to the specified action method.
Comments
Post a Comment