开发者

How to access web.config values?

开发者 https://www.devze.com 2023-02-25 12:26 出处:网络
I wrote this in my web.config now how to access host,port in my code 开发者_如何学JAVAI am using like this but it is unable to read pls help me

I wrote this in my web.config now how to access host,port in my code

开发者_如何学JAVA

I am using like this but it is unable to read pls help me

string smtphost = ConfigurationManager.AppSettings["host"].ToString();

<mailSettings>
    <smtp from="mail.crmprocorp.com" deliveryMethod="Network">
       <network 
          defaultCredentials="false" 
          enableSsl="false" 
          host="smtp.gmail.com" 
          port="25" 
          password="password" 
          userName="xyz@gmail.com"/>
    </smtp>
</mailSettings>


You have to use the ConfigurationManager and its method GetSection to do this.

MSDN docs: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection.aspx


Try the following to get the MailSettingsSectionGroup (assuming this is a web application)

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
string smtpHost = settings.Smtp.Network.Host;
0

精彩评论

暂无评论...
验证码 换一张
取 消