如何读取MaxRequestLength的值----(.net2.0)

本文介绍了如何在.NET 2.0环境中通过配置Web.Config文件来调整HTTP请求的最大长度,并提供了一种通过AppSettings读取这一配置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


在.net2.0中新增类HttpRuntimeSection :

static public double getMaxlength()
... {
System.Web.Configuration.HttpRuntimeSectionhrs
= new System.Web.Configuration.HttpRuntimeSection();
/**/ /// /returnhrs.MaxRequestLength*1024;
return hrs.MaxRequestLength;
}


但上述方法读到的值却始终是4096,查阅MSDN,得知System.Web.Configuration(.net2.0中新增的类)的属性和方法除了AppSettings之外均只能读取"不可自定义的属性和功能 ",没有更好的办法,只好折衷:用.net1.1的方法解决:

static public string getMaxlengthByAppSetting()
... {
// 先在Web.Config文件中增加一个key**************************************** //
// <appSettings>
// <addkey="maxRequestLength"value="1550000"/>
// </appSettings>
// 再来读取这个值,遗憾的是,这样只能写两遍,即
// <system.web>
// <httpRuntimemaxRequestLength="1550000"executionTimeout="3600"/>
// </system.web>
// 这两个值必须保持一致。
Configurationconfig = WebConfigurationManager.OpenWebConfiguration( " ~ " );
return config.AppSettings.Settings[ " maxRequestLength " ].Value;

 
/**/ /* *********************以下为修改某个AppSettings的Key值。
////Configurationconfig=WebConfigurationManager.OpenWebConfiguration("~");
////if(config.AppSettings.Settings["MyAppKey"]==null)
////config.AppSettings.Settings.Add("MyAppKey","Hello!");
////else
////config.AppSettings.Settings["MyAppKey"].Value="Hello2!";
////config.Save();
*/


}



不知有没有更好的办法??

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值