存储我的asp.net MVC应用程序内的配置设置最佳途径 [英] Best Way to store configuration setting inside my asp.net mvc application

查看:102
本文介绍了存储我的asp.net MVC应用程序内的配置设置最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一些API调用到其他Web应用程序的asp.net MVC Web应用程序。但是目前我储存我的code里面的API的用户名,密码API和API URL。如下: -

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
                    {

但我的存储code里面的这些设置不会在灵活的情况下,我需要改变API调用设置,它是不安全的。那么,什么是存储这些设置的最佳方式。我想建立两个数据库表一个用于存储URL(也有可能是多个URL罪将来)。而另一表来存储用户名和密码。所以创建数据库表来存储这些设置的正确方法。

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.

推荐答案

您应该使用Web.Config文件(的配置API )来存储这些设置。这样,您就可以不必每次都重新编译整个应用程序更新设置。这是存储在Web(MVC或Web表单)应用程序设置最标准的方式,并为您的可能性的加密敏感设置透明您的应用程序。

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.

您可以通过访问存储的设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager%28v=vs.100%29.aspx\">WebConfigurationManager类。 的在这里你可以找到如何使用它的一些例子。

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

code样品:<​​/ STRONG>

Code sample:

Web.config文件的appSettings:

Web.config appSettings:

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

C#code:

C# Code:

string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"];

这篇关于存储我的asp.net MVC应用程序内的配置设置最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆