阅读与在 Web.config 中编辑配置

发布于 2024-11-04 18:33:47 字数 1222 浏览 1 评论 0原文

我有一些设置需要从我当前正在处理的网站的管理面板进行编辑。我认为将这些设置放在 web.config 中是有意义的(或者我应该将它们放在其他地方吗?)。所以无论如何,我正在尝试编写必要的代码来执行此操作,但我被卡住了...这是我第一次真正需要这样做...:)这是我到目前为止所得到的:

appSettings Web.config 中的部分:

  <appSettings>
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="InvitationOnly" value="true" />
    <add key="MaintainanceMode" value ="false"/>
  </appSettings>

这是我试图编写的一个类,以便轻松检索和修改将放置在 appSettings 部分中的某些值:

public static class SiteSettings
{
    public static bool InvitationOnly
    {
        get
        {
            var invitation = WebConfigurationManager.AppSettings["InvitationOnly"];
            return Convert.ToBoolean(invitation);
        } 
        set
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            var appSettings = config.GetSection("appSettings") as AppSettingsSection;
            if(appSettings != null)
            {
                //got stuck here...
            }
        }
    }
}

我走在正确的轨道上吗?我如何从这里继续?

顺便问一下,将站点设置放在 web.config 中有多安全?我应该担心什么吗?

谢谢。

I have a few settings that need to be edited from the admin panel of the site I'm currently working on. I thought it makes sense to place those settings inside the web.config (or should I place them somewhere else?). So anyway, I'm trying to write the necessary code to do this, but I got stuck... This is the first time I actually needed to do this so... :) Here's what I've got so far:

appSettings section inside the Web.config:

  <appSettings>
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="InvitationOnly" value="true" />
    <add key="MaintainanceMode" value ="false"/>
  </appSettings>

And here's a class I'm trying to write to allow for easy retrieval and modification of some values that will be placed inside the appSettings section:

public static class SiteSettings
{
    public static bool InvitationOnly
    {
        get
        {
            var invitation = WebConfigurationManager.AppSettings["InvitationOnly"];
            return Convert.ToBoolean(invitation);
        } 
        set
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            var appSettings = config.GetSection("appSettings") as AppSettingsSection;
            if(appSettings != null)
            {
                //got stuck here...
            }
        }
    }
}

Am I on the right track? How do I continue from here?

And by the way, how safe is it to place site settings inside the web.config? Should I be worried about anything?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-11-11 18:33:47

您绝对不想修改 web.config 文件,因为每次这样做您的应用程序都会重新启动。如果 web.config 被修改,ASP.NET 会立即卸载 AppDomain。 web.config 用于存储应用程序的静态、只读配置。如果您需要存储必须修改的设置,则 web.config 不是正确的位置。您可以使用自定义文件、会话、cookie、数据库……

You definitely don't want to modify the web.config file as every time you do so your application will simply restart. If the web.config is modified ASP.NET immediately unloads the AppDomain. The web.config is used to store static, readonly configuration of the application. If you need to store settings that has to be modified, web.config is not the right place. You could use a custom file, session, cookies, a database, ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文