简体   繁体   中英

Can I share same application files between multiple applications where only web.config is different?

I have multiple applications on the same server where only some sections of web config are different. Since everything else is the same; is it possible to map somehow the aspx and other files and to have the single app to share the common files? This will make much easier to manage (es update a file)

In general you might want to use continuous deployment to minimize the effort of updating web apps, see this question for an example.

You could try using symbolic links to handle the duplicate files, see the following answers for futher information: https://stackoverflow.com/a/3070093 , https://stackoverflow.com/a/51679624 .

In my opinion, if you want to make much easier to manage (es update a file, I suggest you could write web deploy command line to deploy the web application to the multiple web sites at once.

I don't suggest you share the files for mutiple IIS server.

Details about how to achieve write web deploy command line to deploy the web application to the multiple web sites at once, I suggest you could refer to this article .

You write the settings in "ini" text file in App_Data folder.

For example, if you have the following sub domains which use the same application folder:

http://sub1.myweb.com
http://sub2.myweb.com
http://sub3.myweb.com

Then you can store the settings or configuration parameters in following folders:

/root/App_Data/sub1/settings.ini
/root/App_Data/sub2/settings.ini
/root/App_Data/sub3/settings.ini

Then, in the Global.asax in Application_Start, where your site starts, load the settings from the App_Data sub-folder into a public static class file.

Besides, storing files in App_Data/Sub-folder, you can also store it in a separated database which stores all settings for different sub-domain.


update 2

All the apps can be identified with their sub-domain, therefore you can write a function to get the sub-domain, for example:

string[] sa = HttpContext.Current.Request.Url.Host.Split('.');
string SubDomain = sa[0];

You may wrap it in a class:

public class config
{
    private static string _subdomain = null;
    public static string SubDomain
    {
        get
        {
            if (_subdomain == null)
            {
                string[] sa = HttpContext.Current.Request.Url.Host.Split('.');
                _subdomain = sa[0];
            }
            return _subdomain;
        }
    }
}

This will allow you to obtain the Sub-Domain like this:

config.SubDomain

Next, is the physical file path of the settings file:

HttpContext.Current.Server.MapPath($"~/App_Data/{config.SubDomain}/settings.ini");

Assume this is your settings class object:

public class AppSettings
{
    public string BranchOfficeName { get; set; }
    public string ManagerName { get; set; }
    public string Address { get; set; }
    public DateTime ExpiryDate { get; set; }
    public int MemberLimit { get; set; }
}

You can load the settings like this:

private static AppSettings _appset = null;
public static void LoadSettings()
{
    string settingsFilePath = HttpContext.Current.Server.MapPath($"~/App_Data/{config.SubDomain}/settings.ini");
    _appset = ConvertIniToClassObject(settingsFilePath);
}

Make it as a public static object:

private static AppSettings _appset = null;
public static AppSettings Settings
{
    get
    {
        if (_appset == null)
        {
            LoadSettings();
        }
        return _appset;
    }
}

public static void LoadSettings()
{
    string settingsFilePath = HttpContext.Current.Server.MapPath($"~/App_Data/{config.SubDomain}/settings.ini");
    _appset = ConvertIniToClassObject(settingsFilePath);
}

Therefore, in your Global.Application_Start, load the settings:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        config.LoadSettings();
    }
}

Then in the rest of your application, you may call the settings parameters something like this:

string output = $@"
<h1>{config.Settings.BranchOfficeName}</h1>
Manager: {config.Settings.ManagerName}<br />
Address: {config.Settings.Address}<br />
Sub-Domain: {config.SubDomain}<br />";
HttpContext.Current.Response.Write(output);

Below is an example of saving the settings file:

public static void SaveSettings()
{
    string ini = ConvertObjectToIni(_appset);
    string settingsFilePath = HttpContext.Current.Server.MapPath($"~/App_Data/{config.SubDomain}/settings.ini");
    System.IO.File.WriteAllText(settingsFilePath, ini);
}

Therefore, in the application, just call:

config.SaveSettings();

Besides storing settings in physical ini file, you can also store it in database:

SELECT * FROM appsettings WHERE subdomain={config.SubDomain}

The key point here is config.SubDomain . Therefore, beside settings, you can now store other files separately according to the sub-domain, for example:

/images/{config.SubDomain}/logo.jpg
/files/{config.SubDomain}/rules.pdf

well, you get the idea.

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