How to redirect to any page (eg Home) from any MVC controller in Episerver? eg after login - redirect to the start page.
To redirect to any MVC action:
public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}
Redirect to the configured start page of the site:
public ActionResult Index()
{
PageData startPage =
ServiceLocator.Current.GetInstance<IContentRepository>().Get<PageData>(ContentReference.StartPage);
// get URL of the start page
string startPageUrl = ServiceLocator.Current.GetInstance<UrlResolver>()
.GetVirtualPath(startPage.ContentLink, startPage.LanguageBranch);
return Redirect(startPageUrl);
}
You can actually still use the RedirectToAction with EPiServer, as long as you also supply the content reference of the page.
public ActionResult Index()
{
return RedirectToAction("ActionName", new { node = ContentReference.StartPage });
}
This has been tested in EPiServer 7.5.440.0.
To redirect to any MVC action:
public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}
Redirect to the configured start page of the site:
public ActionResult Index()
{
PageData startPage =
ServiceLocator.Current.GetInstance<IContentRepository>().Get<PageData>(ContentReference.StartPage);
// get URL of the start page
string startPageUrl = ServiceLocator.Current.GetInstance<UrlResolver>()
.GetVirtualPath(startPage.ContentLink, startPage.LanguageBranch);
return Redirect(startPageUrl);
}
return Redirect(startPageUrl.VirtualPath);
I just tested this on EpiServer 11 and not 10. –
Jalapa You can use RedirectToAction() to redirect to a specific action or Redirect() to a specific URL. In ASP.NET MVC 4 you can use RedirectToLocal() in stead of Redirect(), this is recommended for security reasons when you use a parameter from the querystring.
public ActionResult MyAction()
{
// Use this for an action
return RedirectToAction("Action");
// Use this for a URL
return Redirect("/"); // (Website root)
// Use this for a URL within your domain
return RedirectToLocal("/"); // (Website root)
}
See this following links for more information:
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirect(v=vs.118).aspx
In EpiServer 11 I find it to be cleaner to utilize dependency injection instead of utilizing the ServiceLocator as suggested by Thomas Krantz.
Example using Dependency Injection:
public class SomeController
{
private readonly IContentLoader _contentLoader;
private readonly UrlResolver _urlResolver;
public SomeController(IContentLoader contentLoader, UrlResolver urlResolver)
{
_contentLoader = contentLoader ?? throw new ArgumentNullException(nameof(contentLoader));
_urlResolver = urlResolver ?? throw new ArgumentNullException(nameof(urlResolver));
}
public ActionResult Index(SomePage currentPage)
{
//Retrieving an example startpage type
var startPageContentLink =_contentLoader.Get<StartPageType>(ContentReference.StartPage);
//Getting the redirect link -> Type: VirtualPathData
var redirectLink = urlResolver.GetVirtualPath(startPageContentLink);
return Redirect(redirectLink.VirtualPath);
}
}
Naturally this lacks some validation, when using the content loader (it might throw an exception if your given StartPageType does not exist).
© 2022 - 2024 — McMap. All rights reserved.
return Redirect(startPageUrl.VirtualPath);
I just tested this on EpiServer 11 and not 10. – Jalapa