to:jackyhx(一杯红茶,几句实话) "有一点疑问,user.config是xml文件,那么里面对应user_config这个key的值 是不是有很多个?怎么利用呢?'' web.config <appSetting> <add key="user_config" value="/yourpath/user.config"/> </appSetting> user.config看起来像这样<?xml version="1.0"?> <ModuleSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ConnectionString>server=(local);database=yourdb;uid=sa;pwd=;</ConnectionString> <LockByIP>true</LockByIP> <LockByCookie>true</LockByCookie> <LockDuration>7</LockDuration> </ModuleSettings> 要访问user.config的话,需自己写一个用xml串行化的类。你或者用户根据需要可随时修改配置,这在做大项目的时候或系统可由用户定制的时候,非常有用。这应该算比较高级的用法了。抄的例子:ASP.NET高级编程--Problem-Design-Solution---> 中文版看起来不太爽。下面是一个例子。CONFIG.CS using System; using System.IO; using System.Web; using System.Text; using System.Web.Caching; using System.Xml.Serialization; using System.Xml; using System.Configuration; namespace WebOffice.Configuration { public class ModuleConfig { public static ModuleSettings GetSettings() { HttpContext context = HttpContext.Current; ModuleSettings data = (ModuleSettings)context.Cache["Polls_Settings"]; if (data == null) { XmlSerializer serializer = new XmlSerializer(typeof(ModuleSettings)); try { string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile()); // create a filestream to read the XML document FileStream fs = new FileStream(fileName, FileMode.Open); // use the Deserialize method to retrieve the oject state data = (ModuleSettings)serializer.Deserialize(fs); fs.Close();
context.Cache.Insert("Polls_Settings", data, new CacheDependency(fileName)); } catch (System.IO.FileNotFoundException) { // if the file is not found, return a new empty class data = new ModuleSettings(); } }
return data; } public static void SaveSettings(ModuleSettings data) { string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile()); XmlSerializer serializer = new XmlSerializer (typeof(ModuleSettings));
// serialize the object FileStream fs = new FileStream(fileName, FileMode.Create); serializer.Serialize(fs, data); fs.Close(); } private static string GetSettingsFile() { HttpContext context = HttpContext.Current; // get the file path from the cache string filePath = (string)context.Cache["Polls_SettingsFile"]; // if path is null, get it from web.config if (filePath == null) { // retrieve the value filePath=ConfigurationSettings.AppSettings["Polls_SettingsFile"]; // save into the cache context.Cache["Polls_SettingsFile"] = filePath; } // return the path return filePath; } }
public class ModuleSettings { private string connectionString; private bool lockByIP; private bool lockByCookie; private int lockDuration;
[XmlElement] public string ConnectionString { get { return connectionString; } set { connectionString = value; } } [XmlElement] public bool LockByIP { get { return lockByIP; } set { lockByIP = value; } } [XmlElement] public bool LockByCookie { get { return lockByCookie; } set { lockByCookie = value; } } [XmlElement] public int LockDuration { get { return lockDuration; } set { lockDuration = value; } } }}
to:loging(有了“三个代表”,我的程序没有bug了) 刚好微软的Building Secure ASP.NET Applications文档中 有这个 How To: Store an Encrypted Connection String in the Registry 先把字符串加密后放进注册表中,用时在解出来,可以参考其它文章。Building Secure ASP.NET Applications文档在网上msdn可下载的,喷血推荐给各位。
例如: <appSetting>
<add key="user_config" value="/yourpath/user.config"/> </appSetting>
其中的user.config也是xml文件
有一点疑问,user.config是xml文件,那么里面对应user_config这个key的值
是不是有很多个?怎么利用呢?
1。编码的问题.net中默认的编码是utf-8,而以前的系统(asp)(gb2312)的导入会引起编码的问题,也就是乱码的问题。这在我们开发的时候没有考虑到,但是在测试的时候发现了这个问题。
2。就是和以前的系统(asp等)迁移上,这个问题包括如:以前的上传组件以及引起的配制问题。(对于单独的一个.net项目没有这个问题,但是对于迁移的系统有十几个情况之下,必须要考虑到这个问题)
3。还有就是关于文件上传这方面,我们单独的用一台服务器来存放文件。这个过程中我们在web.config中设置的路径指向另一台服务器出问题。
4。关于ad以及数据库的配制文件等。
在 web.config 中设置连接数据库的连接串。
<appsetting>
<add key="connectionstring" value="initial catalog=yourdb;persist security info=False;user id=youusername;password=yourpassword;Data Source=yourserver's IP;packet size=4096" />
</appsetting>在程序中直接用:ConfigurationSettings.AppSetting["connectionstring"] 就可以拿到了。
当然,需要添加引用: using System.Configuration;后台数据库服务器可以随意变更。
是不是有很多个?怎么利用呢?''
web.config
<appSetting>
<add key="user_config" value="/yourpath/user.config"/> </appSetting>
user.config看起来像这样<?xml version="1.0"?>
<ModuleSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ConnectionString>server=(local);database=yourdb;uid=sa;pwd=;</ConnectionString>
<LockByIP>true</LockByIP>
<LockByCookie>true</LockByCookie>
<LockDuration>7</LockDuration>
</ModuleSettings>
要访问user.config的话,需自己写一个用xml串行化的类。你或者用户根据需要可随时修改配置,这在做大项目的时候或系统可由用户定制的时候,非常有用。这应该算比较高级的用法了。抄的例子:ASP.NET高级编程--Problem-Design-Solution---> 中文版看起来不太爽。下面是一个例子。CONFIG.CS
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Web.Caching;
using System.Xml.Serialization;
using System.Xml;
using System.Configuration;
namespace WebOffice.Configuration
{
public class ModuleConfig
{
public static ModuleSettings GetSettings()
{
HttpContext context = HttpContext.Current; ModuleSettings data = (ModuleSettings)context.Cache["Polls_Settings"]; if (data == null)
{
XmlSerializer serializer = new XmlSerializer(typeof(ModuleSettings));
try
{
string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
// create a filestream to read the XML document
FileStream fs = new FileStream(fileName, FileMode.Open);
// use the Deserialize method to retrieve the oject state
data = (ModuleSettings)serializer.Deserialize(fs);
fs.Close();
context.Cache.Insert("Polls_Settings", data, new CacheDependency(fileName));
}
catch (System.IO.FileNotFoundException)
{
// if the file is not found, return a new empty class
data = new ModuleSettings();
}
}
return data;
}
public static void SaveSettings(ModuleSettings data)
{
string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
XmlSerializer serializer = new XmlSerializer (typeof(ModuleSettings));
// serialize the object
FileStream fs = new FileStream(fileName, FileMode.Create);
serializer.Serialize(fs, data);
fs.Close();
} private static string GetSettingsFile()
{
HttpContext context = HttpContext.Current;
// get the file path from the cache
string filePath = (string)context.Cache["Polls_SettingsFile"];
// if path is null, get it from web.config
if (filePath == null)
{
// retrieve the value
filePath=ConfigurationSettings.AppSettings["Polls_SettingsFile"];
// save into the cache
context.Cache["Polls_SettingsFile"] = filePath;
}
// return the path
return filePath;
} }
public class ModuleSettings
{
private string connectionString;
private bool lockByIP;
private bool lockByCookie;
private int lockDuration;
[XmlElement]
public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
} [XmlElement]
public bool LockByIP
{
get { return lockByIP; }
set { lockByIP = value; }
} [XmlElement]
public bool LockByCookie
{
get { return lockByCookie; }
set { lockByCookie = value; }
} [XmlElement]
public int LockDuration
{
get { return lockDuration; }
set { lockDuration = value; }
}
}}
ASP.NET Web站点高级编程--Problem-Design-Solution-
如
<appsetting>
<add key=",,," value="" />
</appsetting>
然后用
ConfigurationSettings.AppSettings[",,,"],楼上的兄弟都说了。补充一下:这几天看ASP.NET Web站点高级编程--Problem-Design-Solution,
就这一点说说我的体会:
把配置文件分类是个很好的想法,因为web.config的内容很重要,这样可以隔离危险操作,可以让部分用户修改特定文件,而web.config不用做任何修改!刚刚在亚马逊网上书店看读者评价--五星。看来花7折(原价55)买得不亏。
刚好微软的Building Secure ASP.NET Applications文档中
有这个
How To:
Store an Encrypted Connection String in the Registry
先把字符串加密后放进注册表中,用时在解出来,可以参考其它文章。Building Secure ASP.NET Applications文档在网上msdn可下载的,喷血推荐给各位。