2

I have shared appsettings in SharedSettings folder. I am deploying all the asp.net core application in sub directory.

For development, I have created as site with the following directory

c://Website

Here I am keeping my sharedsettings.

But I am developing application from another folder. Here I have configured my application to run as sub application in the web sites. Folder could be c://Applications/App1

I want to include the sharedsettings.json in my application. So I have tried the following approach.

.ConfigureAppConfiguration((hostingContext, config) =>
{
    var env = hostingContext.HostingEnvironment;

    var sharedFolder = Path.Combine(env.ContentRootPath, "..", "Shared");

    config
        .AddJsonFile(Path.Combine(sharedFolder, "SharedSettings.json"), optional: true) // When running using dotnet run
        .AddJsonFile("SharedSettings.json", optional: true) // When app is published
        .AddJsonFile("appsettings.json", optional: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    config.AddEnvironmentVariables();
})

But whenever I have tried to get the hosted url path, It always current application hosted directory. (I understand the reason for the result).

But I don't know how to get the root directory path where it is pointing to the different location.

Reference: https://andrewlock.net/sharing-appsettings-json-configuration-files-between-projects-in-asp-net-core/

Deployed Website Structure

2
  • any update? Does my reply has helped you? Commented Oct 7, 2020 at 1:37
  • @BrandoZhang, thank you for your concern. I haven't try yet. I will try and surely update to you.
    – Jeeva J
    Commented Oct 7, 2020 at 7:18

1 Answer 1

1

According to your description, the only way to achieve your requirement is using Microsoft.Web.Administration library to select the site root path according to the asp.net core application root path.

Notice: If you want to use Microsoft.Web.Administration, you should make sure your application pool contains enough permission to access the applicationhost.config file.

Normally, we will need localsystem permission. About how to modify application pool permission, you could refer to this article.

Codes:

            string path = env.ContentRootPath;
            ServerManager mgr = new ServerManager();
            //the root path is the parent website's root path
            string rootpath = "";
            foreach (var site in mgr.Sites)
            {
                foreach (var application in site.Applications)
                {
                    foreach (var virtualDirectory in application.VirtualDirectories)
                    {
                        if (virtualDirectory.PhysicalPath == path)
                        {
                            rootpath = site.Applications[0].VirtualDirectories.Where(x=>x.Path=="/").Single().PhysicalPath;
                        }
                        
                    }
                }
            }

Not the answer you're looking for? Browse other questions tagged or ask your own question.