I decided to use Properties.Settings to store some application settings for my ASP.net project. However, when trying to modify the data, I get an error The property 'Properties.Settings.Test' has no setter
, since this is generated I have no idea what I should do to change this as all my previous C# Projects have not had this issues.
问问题
6375 次
3 回答
19
我的猜测是您使用Application
范围而不是User
范围定义了属性。应用程序级属性是只读的,并且只能在web.config
文件中进行编辑。
我根本不会Settings
在 ASP.NET 项目中使用该类。当您写入web.config
文件时,ASP.NET/IIS 会回收 AppDomain。如果您定期编写设置,您应该使用其他一些设置存储(例如您自己的 XML 文件)。
于 2013-10-03T06:33:14.250 回答
2
正如 Eli Arbel 已经说过的,您无法从应用程序代码中修改 web.config 中编写的值。您只能手动执行此操作,但随后应用程序将重新启动,这是您不想要的。
这是一个简单的类,您可以使用它来存储值并使它们易于阅读和修改。如果您从 XML 或数据库读取数据,并且取决于您是否要永久存储修改后的值,只需更新代码以满足您的需求。
public class Config
{
public int SomeSetting
{
get
{
if (HttpContext.Current.Application["SomeSetting"] == null)
{
//this is where you set the default value
HttpContext.Current.Application["SomeSetting"] = 4;
}
return Convert.ToInt32(HttpContext.Current.Application["SomeSetting"]);
}
set
{
//If needed add code that stores this value permanently in XML file or database or some other place
HttpContext.Current.Application["SomeSetting"] = value;
}
}
public DateTime SomeOtherSetting
{
get
{
if (HttpContext.Current.Application["SomeOtherSetting"] == null)
{
//this is where you set the default value
HttpContext.Current.Application["SomeOtherSetting"] = DateTime.Now;
}
return Convert.ToDateTime(HttpContext.Current.Application["SomeOtherSetting"]);
}
set
{
//If needed add code that stores this value permanently in XML file or database or some other place
HttpContext.Current.Application["SomeOtherSetting"] = value;
}
}
}
于 2013-10-03T11:24:10.060 回答
-2
这里:http: //msdn.microsoft.com/en-us/library/bb397755.aspx
是您问题的解决方案。
于 2013-10-03T05:48:08.093 回答