Modelon Solutions

A fresh take on your ideas

Azure Web Sites and ~/App_Data

Published by

on

A few weeks ago, I had to demo an older web site to a client and chose to use Azure Web Sites to rapidly deploy outside of my laptop and make the web site available quickly over the internet.

One small challenge though, the web site used data in a directory on the filesystem, which would certainly not be available on the Azure. More than that, the code to open the data was meant to be reusable and was not in an assembly that references ASP.NET, hence no way to call things like Server.MapPath()…

After a few minutes of searching, this is what I found

        public string GetDirectory()
        {
            var directory = Settings.Default.Directory;

            //substitute ~\app_data
            var appDataPath = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            if (!string.IsNullOrEmpty(appDataPath) && directory.ToLower().StartsWith(@"~\app_data"))
                directory = directory.ToLower().Replace(@"~\app_data", appDataPath);

            return directory;
        }

Leave a comment