Kentico Xperience 13 documentation and ASP.NET Core

Most documentation about running Xperience applications under ASP.NET Core can be found in a dedicated section: Developing Xperience applications using ASP.NET Core. The rest of the documentation still applies, but some code samples and scenarios might need slight modifications for Core projects.

Certain pages allow you to switch between Core and MVC 5 content using a selector located under the page heading.

All major differences between the MVC 5 and Core platforms are summarized in Migrating to ASP.NET Core.

×

Adding an administration redirect to ASP.NET Core sites

To give content editors and other staff easier access to the administration application from the frontend Core site, you can set up a redirection mechanism. The redirect can give users the illusion of working in a single application. For example, you can set it up so that the administration application is available under ~/admin, similar to other content management systems.

To configure the redirection mechanism, we recommend using the URL rewriting middleware together with a custom rewrite rule:

  1. Add the rewriting middleware to your application's middleware pipeline.
    • For optimal performance, add the middleware before the routing middleware (UseRouting). The rewrite rule performs a permanent (HTTP 301) redirection, which renders the routing process pointless. Outside of this, the specific placement of the middleware largely depends on the configuration of your middleware pipeline. For more information, refer to Microsoft's documentation on this topic.

  2. Create a new URL rewriting rule. This example uses a rule called AdminRedirect.

    Rewrite rule
    using System;
    using System.Net;
    
    using Microsoft.AspNetCore.Rewrite;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Net.Http.Headers;
    
    
    public class AdminRedirect : IRule
    {
        private readonly string adminUrl;
    
        public AdminRedirect(IConfiguration configuration)
        {
            // Retrieves the 'AdministrationUrl' application setting from the application's configuration providers (e.g., appsettings.json)
            adminUrl = configuration["AdministrationUrl"] ?? String.Empty;
        }
    
        public void ApplyRule(RewriteContext context)
        {
            if (String.IsNullOrEmpty(adminUrl))
            {
                return;
            }
    
            var request = context.HttpContext.Request;
    
            // Redirects requests leading to '~/admin' to the URL specified in the 'AdministrationUrl' setting
            // For example: https://administration.mydomain.com
            if (request.Path.Value.TrimEnd('/').Equals("/admin", StringComparison.OrdinalIgnoreCase))
            {
                var response = context.HttpContext.Response;
    
                response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                response.Headers[HeaderNames.Location] = adminUrl;
                context.Result = RuleResult.EndResponse;
            }
        }
    }

  3. Add the URL of the site's corresponding administration application under the AdministrationUrl settings key. You can place the key into any configuration provider used by your application (e.g., appsettings.json).

    Application configuration file (in the .json format)
    {
    	...
    	"AdministrationUrl": "administration.mydomain.com",
    	...
    }
  4. Add the create rule to the collection of URL rewriting rules used by the middleware

    • The rewrite rule requires the IConfiguration provider to retrieve the URL of the administration interface from application settings. You may need to extend the signature of the Configure method with the IConfiguration parameter. This ensures the IConfiguration provider is available (provided via dependency injection) during application initialization.

      Application startup class
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
      {
          ...
      
          app.UseRewriter(new RewriteOptions()
                              .Add(new AdminRedirect(configuration)));
      
      `	...
      
          app.UseRouting();
      
          ...
      }
  5. Compile your project.

The redirection rule is now registered and ready. Requesting ~/admin redirects the request to the URL specified by the AdministrationUrl setting. You can modify both the path under which the redirection occurs and the name of the setting key to suit your needs.



Was this page helpful?