Redirect to another any episerver page from MVC controller?
Asked Answered
K

4

6

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

Khedive answered 11/3, 2014 at 13:24 Comment(0)
B
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);
}
Brandish answered 11/3, 2014 at 13:44 Comment(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.Jalapa
S
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.

Scale answered 17/3, 2014 at 17:19 Comment(1)
I really like this!Outlawry
B
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);
}
Brandish answered 11/3, 2014 at 13:44 Comment(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.Jalapa
F
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

Ferrick answered 11/3, 2014 at 13:49 Comment(0)
J
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).

Jalapa answered 13/2, 2018 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.