Global.asax、全局变量和使用代码编辑

发布于 2024-07-22 04:42:10 字数 615 浏览 9 评论 0原文

我有两个问题:

1)我的 global.asax 文件中声明了我网站的一些全局变量。 它们是简单的、小的键/值对,而且数量很少。 对于较小且需要我网站上几乎每个页面访问的值来说,这是一个很好的做法吗? 将它们存储在数据库中并需要进行数据库查找似乎认为这会浪费资源来获取不会快速更改的值。

2)如果其中一个值每周更改一次,是否可以允许用户通过表单或其他方式编辑全局变量?

示例:

<script runat="server">

  Overloads Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

      Application("default_scoring_id") = 3
      Application("week_current") = 3

  End Sub

</script>

在上面的示例中,系统需要知道当前日期是在哪一周(共 15 周)。 因此,每周一早上,“week_current”值需要更改一次。

我可以轻松做到这一点,但是有没有办法让用户无需接触代码即可更改此值?

I have two questions:

1) I have a few global variables for my website declared in my global.asax file. They are simple, small key/value pairs and there are only a few of them. Is this a good practice for values that are small and need to be accessed by almost every page on my website? Storing them in the database and requiring a db lookup seems as thought it would waste resources for values that don't change rapidly.

2) If one of the values changes once per week, is it possible to allow a user to edit the global variable with a form or some other means?

Example:

<script runat="server">

  Overloads Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

      Application("default_scoring_id") = 3
      Application("week_current") = 3

  End Sub

</script>

In the above example, the system needs to know which week (out of 15 of them) that the current date is within. So, once every Monday morning, the "week_current" value needs to change.

I can easily do this, but is there a way to give a user access to changing this value without touching the code?

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

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

发布评论

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

评论(6

谈下烟灰 2024-07-29 04:42:10

典型的做法是将这些放入可编辑的 web.config 中。 这些 值将在每个页面上可用,但可以编辑文件。

The typical practice is to put these into the web.config, which can be edited. These <appSettings> values will then be available on every page, yet the file can be edited.

清晨说晚安 2024-07-29 04:42:10

1) 好的做法通常是将这些值存储在 web.config 中。 请参阅此处此处获取一些指南。

2) a) 如果您将其存储在 web.config 中,则无需重新编译即可轻松更新,并且 Web 应用程序应立即获取更改。

b) 更新“周数”之类的内容真的需要手动过程吗? 这听起来有点容易出错,我建议如果可能的话,通过根据当前日期进行计算来自动执行此操作。

1) Good practice is usually to store those sort of values in the web.config. See here and here for some guides.

2) a) If you store this in the web.config it will be easily updatedable without the need for recompiling and the web application should immediatly pick up the changes.

b) Does updating something like the 'week number' really need to be a manual process? It sounds a bit error prone and i would suggest automating this if at all possible by calculating it based on the current date.

乖不如嘢 2024-07-29 04:42:10

我会考虑使用内置的缓存(System.Web.Caching.Cache),

这样您就可以将它们存储在您想要的任何位置(例如数据库中),轻松地从应用程序内更改它们,并进行快速且廉价的检索。

从继承自 BasePage 的类中使用给定的 Cache 对象(例如 Cache.Add(..)),从其他地方使用 HttpContext.Current.Cache(例如 HttpContext.Current.Cache.Remove(Key))

I would consider using the built in Cache (System.Web.Caching.Cache)

That way you can store them where ever you want (say in a database), change them from within the app easily, and have quick and cheap retrieval.

From within a class which inherits from BasePage use the given Cache object (eg Cache.Add(..)) and from elsewhere use HttpContext.Current.Cache (eg. HttpContext.Current.Cache.Remove(Key))

小女人ら 2024-07-29 04:42:10

其他答案提出了可以完成并且必须完成的不同方式。 但是,即使这样,如果您想允许用户编辑全局变量,您也必须在共享对象上获取锁定或互斥体,更改全局变量的值,然后释放锁定或互斥体。

lock(globalsharedobject) //This is C# code.
{
    Application("default_scoring_id") = 3;
}

The other answers suggest the different ways this can be done, and must be done. But, even then if you want to allow the user to edit the global variable, you'll have to take a lock or Mutex on a shared object, change the value of your global variable, and release the lock or Mutex.

lock(globalsharedobject) //This is C# code.
{
    Application("default_scoring_id") = 3;
}
遗失的美好 2024-07-29 04:42:10

Web.config是.NET方式,或者ASP.NET方式,它并不总是最有效或最合适的。

Global.asax 文件不仅仅是一些事件,您可以将静态数据放入 System.Web.HttpApplication 子类的任何类中,并在 Global.asax 文件中继承它。

HttpSessionState 和 HttpApplicationState 是经典 ASP 时代的遗留物,您最好避免使用它们,因为它们没有真正的用途。

根据对象的类型 (System.Type),您可以设计自己的强类型对象来存储有关应用程序和会话的信息,对于应用程序级数据,一堆静态字段就足够了。

它们必须是静态的,因为每个 HttpModule 以及 HttpApplication 实例都是池对象,因此为了避免这种混乱,请确保您的持久数据存储在一个静态或多个静态字典中。 但修改这些集合时请注意并发问题。 一个好的策略是锁定对象,仅在修改对象期间锁定对象,并确保在修改集合时不调用任何其他代码,这是一个简单的交换习惯用法,在这里可能会有所帮助,它速度快,而且非-死锁保证。

Web.config is the .NET way, or ASP.NET way, it's not always the most efficent or most suitable.

You're Global.asax file is much more than some events, you can put static data in any class that subclass System.Web.HttpApplication and inherit that in your Global.asax file.

The HttpSessionState and HttpApplicationState are relics, from the classic ASP time and you would do well to avoid them, becuase the serve no real purpose.

Depending on the type (System.Type) of your objects you can design your own strongly typed objects that store information about your application and session, for application level data a bunch of static fields would be enough.

They have to be static becuase each HttpModule as well as HttpApplication instance are pooled object, so to avoid that confusion, make sure your persistent data is stored in a static or several static dicionaries. But be aware of concurrency issues when modyfying these collections. A good strategy is to lock the object, only for the duration you're modifying it and make sure you don't call any other code while modyfiny the collection, a simple swap idiom, might be helpful here, it's fast and it's a non-deadlock guarntee.

不醒的梦 2024-07-29 04:42:10
<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
    // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
    //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
    // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {

    // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

    }
    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    Context.Items.Add("Request_Start_Time", DateTime.Now);
    }
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
    TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);
    Context.Response.Write("<b>Request Processing Time: From Global.asax file " + tsDuration.ToString());
    Application["time"] = tsDuration.ToString();
    }

</script>
<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
    // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
    //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
    // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {

    // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

    }
    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    Context.Items.Add("Request_Start_Time", DateTime.Now);
    }
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
    TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);
    Context.Response.Write("<b>Request Processing Time: From Global.asax file " + tsDuration.ToString());
    Application["time"] = tsDuration.ToString();
    }

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