DaniKR
DaniKR

Reputation: 2448

What is the correct way to put parameters in web.config in MVC - ASP.NET

I have create this mail send form with MVC 4 in ASP.NET. Now I whant to put (use) parameters for smpt, username, password and port number in my Web.config. I have try this solution:

In Web.config:

<add key="smptserver" value="smptABC" />
<add key="username" value="usernameABC" />
<add key="password" value="passwordABC"/>
<add key="portNum" value="123"/>

In my Controller.cs I have this:

using System.Configuration;

...

string host = ConfigurationManager.AppSettings["smptserver"];
string user = ConfigurationManager.AppSettings["username"];
string pass = ConfigurationManager.AppSettings["password"];
string port = ConfigurationManager.AppSettings["portNum"];

using (var client = new SmtpClient
{
    Host = host,
    Port = port,
    EnableSsl = true,
    Credentials = new NetworkCredential(user, pass),
    DeliveryMethod = SmtpDeliveryMethod.Network
})
{ ...

I have also found this solution (all same, but only this is changed):

string host = ConfigurationSettings.AppSettings["smptserver"];
string user = ConfigurationSettings.AppSettings["username"];
string pass = ConfigurationSettings.AppSettings["password"];
string port = ConfigurationSettings.AppSettings["portNum"];

[other is same]

What are the diffrences between "ConfigurationManager" and "ConfigurationSettings"? Is this a proper way to set parameters with Web.config?

Thanks for explanation.

Upvotes: 0

Views: 5378

Answers (1)

David
David

Reputation: 218960

According to the documentation, ConfigurationSettings exposes deprecated (marked as "obsolete") functionality. (Your code should be generating warning messages at compile time to indicate this. If it's not, turn warnings back on.)

ConfigurationManager is preferred.

Upvotes: 2

Related Questions