• 配置文件的继承与覆盖: Machine.config / Web.config /子目录 Web.config


    C#有三种级别的配置文件:

    机器级别      machine.config  在 C:WindowsMicrosoft.NETFramework64v4.0.30319Configmachine.config 中

    应用程序级别的     web.config    

    子级别的      web.config    在子目录中     

    她们依次具有继承和覆盖关系:  机器级别 >  应用程序级别 > 子级别

    (1)application中可以读取到machine级别的配置信息,子目录中可以读取到application和machine级别的配置信息,所以依次具有继承关系。

    (2)同时因为子配置信息 会覆盖父级配置信息,所以需要remove移除以前的配置信息 或者clear父级配置信息。

    (3)application级别不能读取子目录的配置信息,因为继承是单方向的。

    //application级别的示例:

    <connectionStrings>
    <remove name="LocalSqlServer"/>
    <!--<clear/>-->
    <add name="LocalSqlServer" connectionString="hahhahhahah"/>
    <add name="dbconnStr" connectionString="Data Source=(DESCRIPTION =

    namespace DIDAO.Admin
    {
        /// <summary>
        /// testConfig 的摘要说明
        /// </summary>
        public class testConfig : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
                string conCurr = ConfigurationManager.ConnectionStrings["dbconnStr"].ConnectionString;
                //如果机器级别有这个name,而应用程序也有这个name,就会报错,需要remove移除机器级别的name 或clear清楚机器级别的所遇配置。
                string conMach = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
                //子类级别的配置文件 : 必须在子目录级别才能读取
                string conChild = ConfigurationManager.ConnectionStrings["child"].ConnectionString;
    
                context.Response.Write("应用程序级别的config:"+conCurr+"<br/>");
                context.Response.Write("机器级别的config:" + conMach + "<br/>");
                context.Response.Write("子级别的config:" + conChild + "<br/>");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    //子目录级别的示例:

    <connectionStrings>
    <add name="child" connectionString="asasasasasasaaas"/>
    </connectionStrings>

    namespace DIDAO.Admin.testDir
    {
        /// <summary>
        /// testChild 的摘要说明
        /// </summary>
        public class testChild : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                context.Response.ContentType = "text/html";
                string conCurr = ConfigurationManager.ConnectionStrings["dbconnStr"].ConnectionString;
                //如果机器级别有这个name,而应用程序也有这个name,就会报错,需要remove移除机器级别的name 或clear清楚机器级别的所遇配置。
                string conMach = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
                //子类级别的配置文件 : 必须在子目录级别才能读取
                string conChild = ConfigurationManager.ConnectionStrings["child"].ConnectionString;
    
                context.Response.Write("应用程序级别的config:" + conCurr + "<br/>");
                context.Response.Write("机器级别的config:" + conMach + "<br/>");
                context.Response.Write("子级别的config:" + conChild + "<br/>");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    常用的逻辑控制器。
    map集合迭代。
    [译]如何在.NET Core中使用System.Drawing?
    使用.Net Core 2.1开发Captcha图片验证码服务
    ife 零基础学院 day 2
    ife 零基础学院 day 1
    Visual Studio 2017
    Visual Studio 2017
    在Windows 10上利用seafile搭建个人云服务
    Web网站配置Gzip,压缩js css文件
  • 原文地址:https://www.cnblogs.com/adolphyang/p/4833349.html
Copyright © 2020-2023  润新知