Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to store configuration setting inside my asp.net mvc application

Tags:

I am working on a asp.net mvc web application that perform some API calls to other web applications. but currently I am storing the API username, API password and API URL inside my code. As follow:-

using (var client = new WebClient())
                {
                    //  client.Headers[HttpRequestHeader.Accept] = "AddAsset";
                    var query = HttpUtility.ParseQueryString(string.Empty);
                    foreach (string key in formValues)
                    {
                        query[key] = this.Request.Form[key];
                    }

                    query["username"] = "testuser";
                    query["password"] = "……";
                    query["assetType"] = "Rack";
                    query["operation"] = "AddAsset";
var url = new UriBuilder("http://win-spdev:8400/servlets/AssetServlet");
                    url.Query = query.ToString();
                    try
                    {

But storing these setting inside my code will not be flexible in case I need to change the API call settings, and it is not secure. So what is the best way to store these setting . I was thinking to create two database tables one for storing the URL(and there might be multiple URLs sin the future). And another table to store the username and password. So is creating database tables the right way to store these settings.

like image 635
john Gu Avatar asked Aug 04 '13 15:08

john Gu


People also ask

Which file is used to store settings of the ASP.NET website?

web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website.

How many web config file in asp net MVC application?

There is no restriction to use the web. config file in the asp.net web application. You can have 1 Web. config file per folder .

Where do I put appSettings in web config?

Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.


1 Answers

You should use the Web.Config file (Configuration API) to store these settings. This way you will be able to update settings without having to recompile your entire application every time. That's the most standard way to store settings in a Web (MVC or WebForms) application, and gives you the possibility to encrypt sensitive settings transparently to your application.

You can access stored settings using the WebConfigurationManager class. Here you can find some examples of how to use it.

Code sample:

Web.config appSettings:

<appSettings>
    <add key="ApiUserName" value="MySampleUsername" />
</appSettings>

C# Code:

string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"];
like image 139
Meryovi Avatar answered Oct 01 '22 17:10

Meryovi