اندرخم معجزات وب کانفیگ در ASP.NET
گاهی اوقات ممکنه بخواین از یه سری مقادیر در کل پروژه به دفعات استفاده کنید. روش های مختلفی از Session گرفته تا Xml،ViewState و Cache و … به ذهن خطور میکنه که بسته به شرایط پروژه و نوع اطلاعات و در واقع شرایط اطلاعات تصمیم های مختلفی میشه گرفت. یکی از کارهایی که میشه کرد استفاده از قابلیتی هست که در ASP.NET وب کانفیگ در اختیارمون گذاشته.
شما میتونید با استفاده از تگ appSettings در configuration مقادیر مورد نظرتون رو اضافه کنید باینصورت:
1 2 3 4 5 |
<configuration> <appSettings> <add key="websiteName" value="My New Website"/> <add key="welcomeMessage" value="Welcome to my new Website, friend!"/> </appSettings> |
و به اینصورت مقادیر رو بخونید:
1 2 3 4 5 6 7 8 |
using System.Configuration; protected void Page_Load(object sender, EventArgs e) { lblSiteName.Text = ConfigurationManager.AppSettings["websiteName"]; lblWelcome.Text = ConfigurationManager.AppSettings["welcomeMessage"]; } |
در AppSettings شما فقط نوع داده String میتونید تعریف کنید و این مساله باعث ایجاد محدودیت هایی در ذخیره انواع داده های موردنظر شما میشه. یه روش کاملتر اینه که از configSections استفاده کنید. در این روش شما با تعریف یه ساختار مشخص از اطلاعاتی که لازم دارید میتونید کارهای خارق العاده ای رو انجام بدید.
بعنوان مثال متناسب با نیازتون یه تگ به اسم orderService با ویژگی های ذیل تعریف می کنید:
1 2 |
<orderService available="true" pollTimeout="00:01:00" location="tcp://OrderComputer:8010/OrderService"/> |
زیبایی روش به اینجاش هست که شما با یه کلاس سر و کار خواهید داشت طبق مشخصه های اون تگ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class OrderService : ConfigurationSection { [ConfigurationProperty("available", IsRequired = false, DefaultValue = true)] public bool Available { get { return (bool)base["available"]; } set { base["available"] = value; } } [ConfigurationProperty("pollTimeout", IsRequired = true)] public TimeSpan PollTimeout { get { return (TimeSpan)base["pollTimeout"]; } set { base["pollTimeout"] = value; } } [ConfigurationProperty("location", IsRequired = true)] public string Location { get { return (string)base["location"]; } set { base["location"] = value; } } } |
حالا باید این کلاس رو در configSection ثبت کنید:
1 2 3 4 5 6 7 8 9 |
<configuration> <configSections> ... <section name="orderService" type="OrderService" /> </configSections> <orderService available="true" pollTimeout="00:01:00" location="tcp://OrderComputer:8010/OrderService"/> <system.web>...</system.web> </configuration> |
دسترسی در این حالت به این صورت خواهد بود:
1 2 3 4 5 6 7 |
Configuration config = WebConfigurationManager.OpenWebConfiguration("/"); OrderService custSection = (OrderService)config.GetSection("orderService"); lblInfo.Text += "Retrieved service information...<br />" + "<b>Location:</b> " + custSection.Location + "<br /><b>Available:</b> " + custSection.Available.ToString() + "<br /><b>Timeout:</b> " + custSection.PollTimeout.ToString() + "<br /><br />"; |
ممکنه شما بخوای زیرمجموعه اون تگ یه آیتم دیگه هم اضافه کنی مثلا به این صورت:
1 2 3 |
<orderService available="true" pollTimeout="00:01:00"> <location computer="OrderComputer" port="8010" endpoint="OrderService" /> </orderService> |
کلاسش اینجوری میشه:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Location : ConfigurationElement { [ConfigurationProperty("computer", IsRequired = true)] public string Computer { get { return (string)base["computer"]; } set { base["computer"] = value; } } [ConfigurationProperty("port", IsRequired = true)] public int Port { get { return (int)base["port"]; } set { base["port"] = value; } } [ConfigurationProperty("endpoint", IsRequired = true)] public string Endpoint { get { return (string)base["endpoint"]; } set { base["endpoint"] = value; } } } |
و باید در کلاس OrderService هم ارتباط دو کلاس رو برقرار کنی:
1 2 3 4 5 6 7 |
[ConfigurationProperty("location", IsRequired = true)] public Location Location { get { return (Location)base["location"]; } set { base["location"] = value; } } |
حالا:
1 |
lblInfo.Text = "<b>Server:</b> " + custSection.Location.Computer; |
موفق باشید.
پ.ن: جهت کسب اطلاعات بیشتر میتونید به کتاب Apress Pro ASP.NET 3.5 مراجعه کنید.
درباره حمیدرضا متقیان
مدیر محصول نرم افزار. علاقه مند به دنیای وب و ارزش آفرینی در کسب و کار.
نوشته های بیشتر از حمیدرضا متقیاناین سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش میشوند.
پاسخی بگذارید