6

How to redirect to any page (eg Home) from any MVC controller in Episerver? eg after login - redirect to the start page.

4 Answers 4

11

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.

1
  • I really like this!
    – Robban
    Jun 16, 2017 at 8:05
10

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);
}
1
  • 1
    In EpiServer 11+ you need to access the VirtualPath on the object. In the above example the code should be: return Redirect(startPageUrl.VirtualPath); I just tested this on EpiServer 11 and not 10.
    – Nift
    Feb 13, 2018 at 13:48
1

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

0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.