web.config配置文件使用總結


  我們在開發web系統的時候,使用web.config進行配置是司空見慣的,那么web.confg到底是什么呢?什么時候使用web.config呢?有幾種使用web.config方式呢? 如果不太明白的話,那這篇文章就帶領大家腦補下。

  》》首先回答第一個問題,web.config是什么?

  是一個XML文本文件。用來存儲ASP.NETWEB的配置信息。修改不需要重啟服務器就可以生效。

 》》 然后第二個問題,什么時候使用web.config?

  網站中很多不確定的可能隨時更改的數據都可以寫到配置文件里面。

 》》有幾種使用web.config的方式呢?

 下面就介紹幾種常見的:

~ connectionStrings配置(這個一般用來配置數據庫連接)

  <connectionStrings>
    <add name="Default" connectionString="Server=.; Database=Inferno;User ID=sa; Password=pwd123;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
  </connectionStrings>

獲取:

  string dbStr=ConfigurationManager.ConnectionStrings["Default"].ConnectionString;

~appSettings配置(這個一般用來配置不確定的或者需要用戶配置的內容)

 <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

獲取

string clientValidationEnabled= ConfigurationManager.AppSettings["ClientValidationEnabled"].ToString();

~自定義配置(這個一般用戶批量配置某些內容)

例如,我下面定義了一個jieDian 鍵值對配置和一個 fileupload 上傳文件集合配置

其中,上面的UploadConditionHandler我對應的是一個類:(這個類需要繼承IConfigurationSectionHandler並實現Create方法)

namespace TestProject
{
    public class UploadConditionHandler : IConfigurationSectionHandler
    {
        public UploadConditionHandler() { }
        public static NameValueCollection ossrules = ConfigurationManager.GetSection("testSectionGroup/jieDian") as NameValueCollection;

        public static Dictionary<string, UploadRequiredParameters> ScreensDictionary = ConfigurationManager.GetSection("testSectionGroup/fileupload") as Dictionary<string, UploadRequiredParameters>;
        public object Create(object parent, object configContext, XmlNode section)
        {
            Dictionary<string, UploadRequiredParameters> screens = new Dictionary<string, UploadRequiredParameters>();

            try
            {
                string name = string.Empty;
                string path_rule = string.Empty;
                string disk = string.Empty;
                string receive_server = string.Empty;
                string ext_array = string.Empty;
                string max_memory_size = string.Empty;
                string enum_bucket_name = string.Empty;
                string max_pixel_height = string.Empty;
                string max_pixel_width = string.Empty;
                foreach (XmlNode childNode in section.ChildNodes)
                {
                    if (childNode.NodeType != XmlNodeType.Element || childNode.Name != "upload")
                        continue;

                    if (childNode.Attributes["name"] != null)
                    {
                        name = childNode.Attributes["name"].Value;
                        if (childNode.Attributes["path_rule"] != null)
                        {
                            path_rule = childNode.Attributes["path_rule"].Value;
                        }
                        if (childNode.Attributes["disk"] != null)
                        {
                            disk = childNode.Attributes["disk"].Value;
                        }
                        if (childNode.Attributes["receive_server"] != null)
                        {
                            receive_server = childNode.Attributes["receive_server"].Value;
                        }
                        if (childNode.Attributes["ext_array"] != null)
                        {
                            ext_array = childNode.Attributes["ext_array"].Value;
                        }
                        if (childNode.Attributes["max_memory_size"] != null)
                        {
                            max_memory_size = childNode.Attributes["max_memory_size"].Value;
                        }
                        if (childNode.Attributes["enum_bucket_name"] != null)
                        {
                            enum_bucket_name = childNode.Attributes["enum_bucket_name"].Value;
                        }
                        if (childNode.Attributes["max_height"] != null)
                        {
                            max_pixel_height = childNode.Attributes["max_height"].Value;
                        }
                        if (childNode.Attributes["max_width"] != null)
                        {
                            max_pixel_width = childNode.Attributes["max_width"].Value;
                        }
                        UploadRequiredParameters upload = new UploadRequiredParameters();

                        upload.PathRule = path_rule;
                        upload.Disk = disk;
                        upload.ReceiveServer = receive_server;
                        upload.ExtArray = ext_array;

                        int maxMemorySize = 0;
                        if (int.TryParse(max_memory_size, out maxMemorySize))
                        {
                            upload.MaxMemorySize = maxMemorySize;
                        }
                        upload.EnumBucketName = enum_bucket_name;
                        int maxPixelHeight = 0;
                        if (int.TryParse(max_pixel_height, out maxPixelHeight))
                        {
                            upload.MaxPixelHeight = maxPixelHeight;
                        }
                        int maxPixelWidth = 0;
                        if (int.TryParse(max_pixel_width, out maxPixelWidth))
                        {
                            upload.MaxPixelWidth = maxPixelWidth;
                        }

                        screens.Add(name, upload);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return screens;
        }
    }
}
View Code

 

好了,上面的步驟完成后,就可以在其它地方調用了:           

 

string jieDian1=UploadConditionHandler.ossrules["JieDian1"];
UploadRequiredParameters res = UploadConditionHandler.ScreensDictionary["UploadRules_Mostly"];

 

 

 

目前就先介紹這么多啦,有什么疑問的或者需要了解的歡迎留言~  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM