![asp.net设置只读](https://i-blog.csdnimg.cn/blog_migrate/a8b0302b349d8dd414bc286216a34209.png)
asp.net设置只读
Requirement:
需求:
Pre-Load the ASP.NET Application object and dont allow other pages in your project to add/modify/delete. Basically, make Application read-only.
预加载ASP.NET应用程序对象,不允许项目中的其他页面添加/修改/删除。 基本上,将应用程序设为只读。
Solution:
解:
private void SetApplicationReadOnlyStatus( bool state ) { Type type = Application.GetType().BaseType; PropertyInfo readPropInfo = type.GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance) ; if( readPropInfo != null ) { readPropInfo.SetValue( Application,Convert.ChangeType(state,readPropInfo.PropertyType ) ,null); } } private void Page_Load(object sender, System.EventArgs e) { Application["MyKey"] = "myValue"; // Set application to readonly so that we will not allow any changes. SetApplicationReadOnlyStatus( true ); try { // Try setting a value Application["MyKey"] = "New value"; Response.Write("You won't see this"); } catch(Exception ex) { // You'll end up here Response.Write(ex.Message); } }
私人无效SetApplicationReadOnlyStatus( bool state) { 类型类型= Application.GetType()。BaseType; PropertyInfo readPropInfo = type.GetProperty( “ IsReadOnly” ,BindingFlags.NonPublic | BindingFlags.Instance); 如果(readPropInfo!= null ) { readPropInfo.SetValue(Application,Convert.ChangeType(state,readPropInfo.PropertyType), null ); } } 私人无效Page_Load(对象发送者,System.EventArgs e) { Application [ “ MyKey” ] = “ myValue” ; //将应用程序设置为只读,这样我们将不允许任何更改。 SetApplicationReadOnlyStatus( true ); 尝试{ //尝试设置一个值Application [ “ MyKey” ] = “新值” ; Response.Write( “您不会看到这个” ); } catch (ex例外) { //您将在这里结束Response.Write(ex.Message); } }
Thanks to Sairama for the code!
感谢Sairama提供的代码!
翻译自: https://www.hanselman.com/blog/set-the-aspnet-application-object-to-readonly
asp.net设置只读