【问题标题】:Properties.Settings has no setterProperties.Settings 没有设置器
【发布时间】:2013-10-09 16:23:18
【问题描述】:

我决定使用 Properties.Settings 来为我的 ASP.net 项目存储一些应用程序设置。但是,在尝试修改数据时,我收到错误 The property 'Properties.Settings.Test' has no setter,因为这是生成的,我不知道应该怎么做才能更改它,因为我以前的所有 C# 项目都没有这个问题。

【问题讨论】:

    标签: c# asp.net settings.settings


    【解决方案1】:

    我的猜测是您使用Application 范围定义了该属性,而不是User 范围。应用级属性是只读的,只能在web.config 文件中编辑。

    我根本不会在 ASP.NET 项目中使用 Settings 类。当您写入web.config 文件时,ASP.NET/IIS 会回收 AppDomain。如果您定期编写设置,您应该使用其他一些设置存储(例如您自己的 XML 文件)。

    【讨论】:

      【解决方案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;
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        这里: http://msdn.microsoft.com/en-us/library/bb397755.aspx

        是您问题的解决方案。

        【讨论】:

        • 这不是问题,我的问题是Settings.Default.LastPayNumber += 1; 说"The Property 'Properties.Settings.LastPayNumner' has no setter"
        • 他在一个 ASP.NET 网站上。您的链接不适用。
        猜你喜欢
        • 2017-08-11
        • 2012-01-26
        • 2013-01-05
        • 2018-01-24
        • 2019-02-22
        • 2017-12-17
        • 2016-03-20
        • 1970-01-01
        • 2013-08-03
        相关资源
        最近更新 更多