简体   繁体   中英

Azure Deployment Slots and URLs in code

I have a txt file in my solution that serves as a template for emails sent by my app. In the file there are links that contain the host URL of my app. When I am deployed to a staging slot in my Azure App Service, the url of the deployment is myapp-staging.azurewebsites.net and to test emails in staging we those links must contain that URL.

In production, the host URL will be myapp.customdomain.com. The url in the text file now needs reflect this url used in the production slot.

Is there a way to use deployment slots and the benefits of swapping if I need to have URLs in the code being deployed to each slot? I looked at the Deployment Slot App Settings but I don't see how I could use those to re-write URLs in those text files.

You should store only the resource path after the server address, in which case you need to get the server url in the code itself and concatenate with the rest of the path it will contain in the file.

To get the current server path you will use:

string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority)

how I could use those to re-write URLs in those text files.

Firstly, you can check and replace the hostURL from the text file dynamically based on WEBSITE_SITE_NAME . The following code snippet is for your reference.

var siteName = Environment.ExpandEnvironmentVariables("%WEBSITE_SITE_NAME%");

string template = System.IO.File.ReadAllText(Server.MapPath("~/Content/email.txt"));

//determine if it is staging slot

if (siteName.IndexOf("-staging") > 0)
{
    //replace hostURL based on siteName

    template = Regex.Replace(template, @"hostURL:myapp.azurewebsites.net", "hostURL:myapp-staging.azurewebsites.net");
    System.IO.File.WriteAllText(Server.MapPath("~/Content/email.txt"), template);
}
else
{
    //replace hostURL based on siteName

    template = Regex.Replace(template, @"hostURL:myapp.azurewebsites.net", "hostURL:myapp.customdomain.com");
    System.IO.File.WriteAllText(Server.MapPath("~/Content/email.txt"), template);
}

Secondly, if possible, you can deploy two separate text files with different hostURL content to production and staging slot.

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