简体   繁体   English

asp.net中的配置设置

[英]configuration settings in asp.net

We have a static html/webform site, the site lacks search functionality, I was able to get yahoo BOSS (Build your Own Search Service) after a few hours yesterday, i got it working (still working on adding missing features like pagination) , I was wondering about the configuration options of the class, as I have a BossSearch.cs in App_Code, with some fields that are set at the top: 我们有一个静态的html / webform网站,该网站缺乏搜索功能,昨天几个小时后,我能够获得雅虎BOSS(建立你自己的搜索服务),我得到它的工作(仍在努力添加像分页一样的缺失功能),我想知道该类的配置选项,因为我在App_Code中有一个BossSearch.cs,其中一些字段设置在顶部:

public class BossSearch
{
    String sResultsPage = "~/searchResults.aspx";
    String sSearchString="";

    String sApiKey = ConfigurationSettings.AppSettings["BossApiKey"];
    String sSite = "www.oursite.com"; //without http://
    String sQuery = "http://boss.yahooapis.com/ysearch/web/v1/{0}%20+site:{1}?appid={2}&format=xml&start={3}&count={4}";
    String sStart = "0";
    Uri address;

    WebProxy webproxy = new WebProxy("http://192.168.4.8:8080");
    bool bUseProxy = true;

    int nResultsPerPage = 10;

    int nTotalResults = 0;
    ...

As you can see, i get the BossApiKey from the web.config file, but all others I have them in the declared in the class, should I put all of them in the web.config file? 正如您所看到的,我从web.config文件中获取了BossApiKey,但是我在类中声明了所有其他文件,我应该将它们全部放在web.config文件中吗? if I'm thinking of reusing the class (should i say class library?) in other websites as well? 如果我想在其他网站上重复使用课程(我应该说类课程?)? can I turn it into a dll and what would the advantages be? 我能把它变成一个dll,它的优点是什么? i read somewhere that a dll has its own config file, is this the way to store those settings? 我读到一个dll有自己的配置文件,这是存储这些设置的方式吗?

Apologies for my ignorance, since I'm not that familiar with developing applications (still studying) 为我的无知道歉,因为我不熟悉开发应用程序(仍在学习)

如果在数据库或web.config中声明所有这些,则每次重新配置搜索引擎时都不需要重新编译

I you're striving for reuse and ease of use, then I would recommend writing a custom configuration section for your control. 我正在努力重用和易用,然后我建议为您的控件编写自定义配置部分 This can be part of the dll you distribute to other application and will allow you to have the ultimate in flexibility and explicit portability to other .net apps. 这可以是您分发给其他应用程序的DLL的一部分,并允许您具有最大的灵活性和对其他.net应用程序的显式可移植性。

Enjoy! 请享用!

What you read about .NET assemblies having their own config files is not absolutely correct; 你读到的.NET程序集有自己的配置文件并不是绝对正确的; a web site has web.config files, one in the root and zero/one in each subdirectory. 一个网站有web.config文件,一个在根目录中,零/一个在每个子目录中。 If a .NET assembly that is in the application calls into the standard config API, it will get its data from that web.config . 如果应用程序中的.NET程序集调用标准配置API,它将从该web.config获取其数据。

The same goes for WinForms apps and the [appname].exe.config file; WinForms应用程序和[appname] .exe.config文件也是如此; any assemblies included that use the standard config API will be getting their data from that. 包含使用标准配置API的任何程序集都将从中获取数据。

All of that is not to say that any assembly could not define its own configuration mechanism which pulls its data from wherever it wants. 所有这一切并不是说任何程序集都无法定义自己的配置机制,从任何地方提取数据。

And yes; 是的; if you intend to reuse this code a good bit, you are thinking along the right lines; 如果你打算重新使用这个代码,那么你正在考虑正确的思路; put the code in its own assembly, and have it get its data from Config files so you do not need to recompile it for each application. 将代码放在自己的程序集中,让它从Config文件中获取数据,这样就不需要为每个应用程序重新编译它。

You should only store the value in a single place in your application. 您只应将值存储在应用程序的单个位置。 For an ASP.NET application, the web.config file is an appropriate place for these kind of things. 对于ASP.NET应用程序,web.config文件是适合这类事情的地方。 You won't need to recompile your application if this value changes. 如果此值发生更改,则无需重新编译应用程序。

If you decide to put your code into a separate class library and still want to use a config file to store your api key, you should note that your appSetting key needs to be entered in the application or web site's config file - you can't define a config file for a class library. 如果您决定将代码放入单独的类库并仍希望使用配置文件来存储api密钥,则应注意您的appSetting密钥需要在应用程序或网站的配置文件中输入 - 您不能为类库定义配置文件。

One other approach that you might find useful would be to make a wrapper class to store your settings. 您可能会发现另一种有用的方法是创建一个包装类来存储您的设置。 You could have class with static methods to look up your appSettings key so that you get a nice, compile time, way to get the api key, rather than typing out the name of your appSettings key everywhere. 您可以使用静态方法查找appSettings键,这样您就可以获得一个很好的编译时间来获取api键,而不是在任何地方输入appSettings键的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM