1

I am having a class library in which I am access a file from the application path using the below code. It wont work on azure as it cannot find the application path. How to make it work azure as it works fine on my local machine.

 private string GetProjectPath()
        {
            string SolutionName = "MyApi.sln";
            //Get name of the target project which we want to test

            var projectName = typeof(TerminationMatchInquiry).GetTypeInfo().Assembly.GetName().Name;

            //Get currently executing test project path
            var applicationBasePath = new Uri((typeof(TerminationMatchInquiry).GetTypeInfo().Assembly.CodeBase)).LocalPath;
            //Find the folder which contains the solution file. We then use this information to find the 
            //target project which we want to test
            DirectoryInfo directoryInfo = new DirectoryInfo(applicationBasePath);
            do
            {
                var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName));
                if (solutionFileInfo.Exists)
                {
                    return Path.GetFullPath(Path.Combine(directoryInfo.FullName, projectName));
                }
                directoryInfo = directoryInfo.Parent;
            }
            while (directoryInfo.Parent != null);

            throw new Exception($"Solution root could not be located using application root {applicationBasePath}");
        }
Youngjae
  • 24,352
  • 18
  • 113
  • 198
maxspan
  • 13,326
  • 15
  • 75
  • 104

1 Answers1

4

To acquire a exact filepath on your webapp, You may need to have trial-and-errors.

Below code is what I used in ASP.NET project. Assume that you have a folder named scenarios and filename variable;

string rootPath;
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
    rootPath = Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot\\bin";
else if (HttpContext.Current != null)
    rootPath = HttpContext.Current.Server.MapPath("~");
else
    rootPath = ".";
string path = rootPath + "\\scenarios\\" + filename;

Let's see values on each condition;

  1. Environment.GetEnvironmentVariable("HOME"): This value is for Azure WebApp. Azure WebApp has default environment variables of host root directory. You can check provided variables as here

  2. HttpContext.Current: This value is for iisexpress with OWIN selfhost when you develop on the desktop.

  3. rootPath = "."; This value is for classic IIS.

So, the best shortcut to find the published filepath could be,

  1. See your file is well deployed on WebApp. You can see the published files with WebApp > Advanced Tools (a.k.a Kudu) menu > Debug Console menu on top > CMD. It will show you explorer-like interface on browser.
  2. Try to write a log with filepath like; Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot\\bin\\" + yourdirectory + "\\" + SolutionName and check the file at there is loaded successfully.
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • @maxspan // `Environment.GetEnvironmentVariable` works for core also. Try it firstly :) `HttpContext.Current` has alternative as this link describes https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core You can omit to check this value as your dev setup works fine. – Youngjae Jun 20 '17 at 05:00