AI agents: the content index is at /llms.txt. Every page is also available as Markdown — send the request header Accept: text/markdown, or append .md to the page URL.
Xperience Docs

Cookie consent

We use necessary cookies (opens in a new tab) to run our website and improve your experience while browsing. Additional cookies are only used with your consent. You may revoke your consent on the Cookies Policy (opens in a new tab) page or in your browser at any time.

Configure (opens in a new tab)
  • Documentation
    InstallationUsing the Xperience interfaceDeveloping websitesConfiguring XperienceManaging usersSecuring websitesDeploying websitesManaging website contentMultilingual websitesMacro expressionsExternal utilitiesIntegrating 3rd party systemsCustom developmentE-commerce featuresOn-line marketing featuresRelease notes - Xperience 13Xperience terminology glossary
  • Tutorial
    Using the Xperience interfaceDeveloper tutorialBusiness tutorialResource hub for marketers
  • API Examples
    ConfigurationContent managementDevelopmentE-commerceOn-line marketingSocial media
  • Community Questions & Answers (opens in a new tab)
  • Contact support (opens in a new tab)
  1. Home
  2. Developing websites
  3. Developing Xperience applications using ASP.NET Core
  4. Starting with ASP.NET Core development
  5. Adding an administration redirect to ASP.NET Core sites

Adding an administration redirect to ASP.NET Core sites


Was this page helpful?

Send us feedback Go back to top

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⁠ (opens in a new tab) 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⁠ (opens in a new tab)) 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⁠ (opens in a new tab) on this topic.
  2. Create a new URL rewriting rule. This example uses a rule called AdminRedirect.

    C#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⁠ (opens in a new tab) used by your application (e.g., appsettings.json).

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

      C#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.