Adding an administration redirect to MVC sites

Optionally, you can set up your MVC site to provide a redirect that sends users to the administration interface of the connected Xperience instance. Such a redirect gives content editors and other staff easier access to the interface where they edit the site’s content and perform other configurations. For example, you can add a redirect for the /admin URL path.

We recommend using the following approach to set up the administration redirect:

  1. Open your MVC project in Visual Studio.

  2. Edit the project’s App_Start\RouteConfig.cs file.

  3. Map a new route according to your URL requirements in the RegisterRoutes method.

    We recommend mapping the administration redirect route in the following position:

    • After Xperience system routes, i.e. after you call the routes.Kentico().MapRoutes() extension method.
    • Before more general routes that you use for the site’s content.
    Example
    
    
    
                 // Redirects to the connected Xperience administration interface if the URL path is '/admin'
                 routes.MapRoute(
                     name: "Admin",
                     url: "admin",
                     defaults: new { controller = "AdminRedirect", action = "Index" }
                 );
    
    
    
     
  4. Edit the MVC project’s web.config file and add a custom key to the appSettings section, with the URL of the related administration instance as the value. Remember to include the /admin path after the administration application’s domain.

    Example
    
    
    
     <appSettings>
         ...
         <add key="CustomAdminUrl" value="https://admin.mydomain.com/admin" />
         ...
     </appSettings>
    
    
     

    You can alternatively set the administration URL directly in the code of the redirection controller. However, a web.config key allows you to manually configure or automatically transform the administration URL without the need to compile the MVC project. This can be useful when deploying the site between environments hosted on different URLs (development, testing, production, etc.).

  5. Create a new controller class in your MVC project, with a GET action that handles the mapped redirect route.

    
    
    
     using System;
     using System.Web.Mvc;
    
     using CMS.Core;
    
    
    
     
    Example
    
    
    
         public class AdminRedirectController : Controller
         {
             private readonly IAppSettingsService appSettingsService;
    
             public AdminRedirectController(IAppSettingsService appSettingsService)
             {
                 this.appSettingsService = appSettingsService;
             }
    
             // GET: Redirects to the administration interface URL of the connected Xperience application
             public ActionResult Index()
             {
                 // Loads the administration interface URL from the 'CustomAdminUrl' appSettings key in the web.config
                 string adminUrl = appSettingsService["CustomAdminUrl"];
    
                 if (!String.IsNullOrEmpty(adminUrl))
                 {
                     // Redirects to the specified administration interface URL 
                     return RedirectPermanent(adminUrl);
                 }
    
                 // If the 'CustomAdminUrl' web.config key is not set, returns a 404 Not Found response 
                 return HttpNotFound();
             }
         }
    
    
    
     
  6. Save the changes and rebuild your MVC project.

When a user now accesses the mapped URL path on your MVC site (for example <domain>/admin), the redirect opens the related administration interface.