彼此进步 2007-12-4 09:33
如何读取MaxRequestLength的值
在.net2.0中新增类HttpRuntimeSection :
[img]http://www.qqread.com/ArtImage/20061126/yw6_1.gif[/img] static public double getMaxlength()
[img]http://www.qqread.com/ArtImage/20061126/yw6_2.gif[/img][img]http://www.qqread.com/ArtImage/20061126/yw6_3.gif[/img] ...{
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] System.Web.Configuration.HttpRuntimeSection hrs =
new System.Web.Configuration.HttpRuntimeSection();
[img]http://www.qqread.com/ArtImage/20061126/yw6_5.gif[/img][img]http://www.qqread.com/ArtImage/20061126/yw6_6.gif[/img] /**/////return hrs.MaxRequestLength * 1024;
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] return hrs.MaxRequestLength ;
[img]http://www.qqread.com/ArtImage/20061126/yw6_7.gif[/img] }
但上述方法读到的值却始终是4096,查阅MSDN,得知System.Web.Configuration(.net2.0中新增的类)的属性和方法除了AppSettings之外均只能读取"不可自定义的属性和功能",没有更好的办法,只好折衷:用.net1.1的方法解决:
[img]http://www.qqread.com/ArtImage/20061126/yw6_1.gif[/img] static public string getMaxlengthByAppSetting()
[img]http://www.qqread.com/ArtImage/20061126/yw6_2.gif[/img][img]http://www.qqread.com/ArtImage/20061126/yw6_3.gif[/img] ...{
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //先在Web.Config文件中增加一个key ****************************************//
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] // <appSettings>
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] // <add key="maxRequestLength" value="1550000"/>
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //</appSettings>
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //再来读取这个值,遗憾的是,这样只能写两遍,即
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //<system.web>
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //<httpRuntime maxRequestLength="1550000" executionTimeout="3600"/>
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //</system.web>
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //这两个值必须保持一致。
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] return config.AppSettings.Settings["maxRequestLength"].Value;
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img]
[img]http://www.qqread.com/ArtImage/20061126/yw6_5.gif[/img][img]http://www.qqread.com/ArtImage/20061126/yw6_6.gif[/img] /**//* *********************以下为修改某个AppSettings的Key值。
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] ////Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] ////if (config.AppSettings.Settings["MyAppKey"] == null)
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //// config.AppSettings.Settings.Add("MyAppKey", "Hello!");
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] ////else
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] //// config.AppSettings.Settings["MyAppKey"].Value = "Hello2!";
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img] ////config.Save();
[img]http://www.qqread.com/ArtImage/20061126/yw6_8.gif[/img] */
[img]http://www.qqread.com/ArtImage/20061126/yw6_4.gif[/img]
[img]http://www.qqread.com/ArtImage/20061126/yw6_7.gif[/img] }
不知有没有更好的办法??