简体   繁体   中英

Force MVC RouteConfig Execution?

In my ASP.NET MVC website, I have code in the RouteConfig.cs file to force the URL to include "www":

using Canonicalize;
using System.Configuration;
using System.Web.Routing;

namespace MyWebsite
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            if (ConfigurationManager.AppSettings["Production"] == "true")
            {
                routes.Canonicalize().Www();
            }
        }
    }
}

I host my website in Azure, which has "deployment slots." I push my code out to a "staging" slot, test, and then "swap" the "staging" slot with the "production" slot.

In the code above, you can see I'm conditionally including routes.Canonicalize().Www(); only for the production slot. This is because the staging slot uses a URL that doesn't allow "www."

The problem is that it seems like the code in the RouteConfig.cs file is only fired on the initial website load. When I load the website, RouteConfig.cs is executed, and routes.Canonicalize().Www(); is correctly excluded. When I swap the slots, the RouteConfig.cs code does not appear to be re-executed, and routes.Canonicalize().Www(); remains excluded, which is not what I want.

Is there a way I can reliably and accurately have routes.Canonicalize().Www(); included only for the production slot?

I would suggest you to use application initialization module in this scenario where you can call you function to add routes.Canonicalize().Www() . The application initialization module has been implemented and is available to use while you swap content between deployment slots. This module has also been implemented regarding all other operations in which a new worker is provisioned (such as auto scale, manual scale or Azure fabric maintenance). Namely, you can proactively perform initialization tasks before your app becomes available.

In order to enable the said module you need to create an applicationInitialization section where you shall define the url to be hit for the initialization task to begin. You have also the option to specify the hostName to be used for the warm up requests, something that may prove really helpful for debugging and monitoring purposes. If not otherwise specified, the “localhost” will be used as host name.

<system.webServer>  
  <applicationInitialization>  
    <add initializationPage="/app/initialize" hostName="warmup-requests.domain.com"/>  
  </applicationInitialization>  
<system.webServer>

Following the above, the swap operation between deployment slots will be completed after the code under “app/initialize” url is executed.

Reference:

https://ruslany.net/2015/09/how-to-warm-up-azure-web-app-during-deployment-slots-swap/ https://feedback.azure.com/forums/169385-web-apps/suggestions/6972595-application-initialization-to-warm-up-specific-pag

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM