Location节点是用来指定子配置的资源。如果在asp.net应用程序中想对某个目录做特殊处理,则可以用该节点来实现。举两个例子。
下面的代码示例演示如何仅将指定页的上载文件大小限制设置为 128 KB。
为指定目录的图片加水印:
四、 针对配置文件的一些编程操作
1. 运行时进行配置文件的修改
这里我们演示对appSettings节点进行修改:
public static void SetAppSetting(string key, string value)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSetting = config.AppSettings;
//如果不存在则添加
if (appSetting.Settings[key] == null)
{
appSetting.Settings.Add(key, value);
}
else//否则修改
{
appSetting.Settings[key].Value = value;
}
config.Save(ConfigurationSaveMode.Full);
}
2. 配置节点的加密
有时候我们要对关键节点进行加密,.net给我们提供了加密的方法,下面我们演示对connectionStrings节点进行加密:
public static void ProtectSection()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection section = config.Sections["connectionStrings"];
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
这里我们使用的是RsaProtectedConfigurationProvider,加密后connectionStrings节点为:
Rsa Key
Sg75NxcTUSMJwZG9ypLUZh9CeSe6Qa1APhxLpZ+EMNWH4lA9AhEEVbzxAgbWvjPGeJoQfONxpkhjeNVZUTrpm9T6dJMU2vQ6EPmXMMF7Lkng62nQ1LOK+gkTbJT8Z3VsprazFteQAwiBhL8GWB4M94kO7bx6P5Ifu6xgXPYdoEQ=
zf47WegBTe8WdP7Pj8104IP1r0UuqNDYIyFppaN5e5TmtZihJQZZyNGW+NZiJqct+q+OdxPWEPRsi/F1tG/URiXypfNhWjZ7o0xps1LoPQgg8Y2mpQ0J3JVOZM8eNjX3jl5ylzPqUK7TsafyuYiht1ljjL2T+WwcQfqnVFlFsXQkQVWde0WMVeqjnSh09rPwJo4o2H9q9T6adaFDZ1WUzBR6eDRudrXsizI8HxdWuU7bD4z2WdQaO6vSUqK0kMep4zAGZOkbUlEjA800Fv0oTDH2fAgVHFXQxxl5EjQSvcjjZ7yViyjsjLJ5RMb1lxjdBQ0msrzQdELMNFVZ2jUbmwv7Pkvk+qcvIbHWTc+o0u4CGqLomsbMUWwZyqIeRXwYmir+CsjIJ4Cm+c6JOleGLsZSKaFRrFE8QjjkixSIvigVTHa8s58VVFKphZo7ZNm91b+8bucaanl8kaBkTsObUDdhfCk/J97gkyZ5BlHEAxnPAT47cj59P1SQqQoGm0gHujyNS4jTgS9JOdb4gBocPiVMBTzG4MhlWGensHLEuu5x9SqNCKYOGuk14Wo9vb4++JiRxCysDmKucGqQXLwTz0FY/IfA1Q16ns+l5MBFYvAoL8hBRHbGWgAodHHsj3UshlP+JI1+buEgxC8O1R0HPNQuIFXQhvGd2RkDQYhCgohyDlPayldl0EPJGYtOAer530s89t52+rU2XH4K84aXbmgClA5VuAzB
以上所有具体操作可以看Demo实例。Asp.net中的web.config配置
原文:http://www.cnblogs.com/lz-blog/p/4388492.html