How many ways to redirect one page, another page in moves dot net core?How many ways to redirect one page, another page in moves dot net core?
There are many ways to redirect from one page to another.There are described as below.
1) RedirectToAction
It is redirected to a specific action method within the same controller or a different controller
Syntax :
return RedirectToAction(“ActionName”, “ControllerName”);
Example
public class HomeController : Controller
{
public IActionResult Index()
{
// Redirect to another action method in the same controller
return RedirectToAction(“About”);
// Redirect to another action method in a different controller
return RedirectToAction(“Index”, “OtherController”);
}
public IActionResult About()
{
// Action method logic
return View();
}
// Other action methods…
}
2. Redirect
Redirect method allows you to redirect to a specific URL
Syntax:
return Redirect(“https://www.example.com");
Example
public class HomeController : Controller
{
public IActionResult Index()
{
// Redirect to a specific URL
return Redirect(“https://www.example.com");
// Redirect to a relative URL
return Redirect(“/OtherController/OtherAction”);
}
// Other action methods…
}
3. RedirectToRoute
Allows you to redirect to a specific route defined in your application’s route configuration
Syntax
return RedirectToRoute(“routeName”);
Example
public class HomeController : Controller
{
public IActionResult Index()
{
// Redirect to a named route
return RedirectToRoute(“MyRoute”);
// Redirect to the default route
return RedirectToRoute(“default”);
}
// Other action methods…
}