简体   繁体   English

我可以在仅 web.config 不同的多个应用程序之间共享相同的应用程序文件吗?

[英]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.我在同一台服务器上有多个应用程序,其中只有 web 配置的某些部分不同。 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?是否有可能 map 以某种方式获取 aspx 和其他文件并让单个应用程序共享公共文件? This will make much easier to manage (es update a file)这将使管理更容易(es更新文件)

In general you might want to use continuous deployment to minimize the effort of updating web apps, see this question for an example.通常,您可能希望使用持续部署来最大程度地减少更新 web 应用程序的工作量,请参阅此问题以获取示例。

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 .您可以尝试使用符号链接来处理重复文件,有关更多信息,请参阅以下答案: 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.在我看来,如果您想更容易管理(es 更新文件,我建议您可以编写 web deploy 命令行以将 web 应用程序一次部署到多个 web 站点。

I don't suggest you share the files for mutiple IIS server.我不建议您共享多个 IIS 服务器的文件。

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 .关于如何实现一次编写web deploy命令行将web应用程序部署到多个web站点的详细信息,建议您可以参考这篇文章

You write the settings in "ini" text file in App_Data folder.您将设置写入 App_Data 文件夹中的“ini”文本文件。

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.然后,在 Application_Start 中的 Global.asax 中,将 App_Data 子文件夹中的设置加载到公共 static class 文件中。

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.此外,将文件存储在 App_Data/Sub-folder 中,您也可以将其存储在单独的数据库中,该数据库存储不同子域的所有设置。


update 2更新 2

All the apps can be identified with their sub-domain, therefore you can write a function to get the sub-domain, for example:所有应用程序都可以通过其子域进行识别,因此您可以编写 function 来获取子域,例如:

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

You may wrap it in a class:您可以将其包装在 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:假设这是您的设置 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:将其设为公共 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:因此,在您的 Global.Application_Start 中,加载设置:

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:然后在您的应用程序的 rest 中,您可以像这样调用设置参数:

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:除了将设置存储在物理 ini 文件中,您还可以将其存储在数据库中:

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

The key point here is config.SubDomain .这里的关键点是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.反正你懂这个意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM