gpt4 book ai didi

caching - 在.net Web应用程序中加载高度重复使用的数据的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-05 01:23:32 25 4
gpt4 key购买 nike

假设我有一个Web应用程序上的导航类别列表。我应该从global.asax的application_onStart中添加一个函数调用,而不是从数据库中选择适合evey用户的用户,以将该数据获取到反复使用的数组或集合中。如果我的数据完全不更改-(经常-编辑),这是最好的方法吗?

最佳答案

您可以将列表项存储在Application对象中。您对application_onStart()正确,只需调用一个方法即可读取数据库并将数据加载到Application对象。

在Global.asax中

public class Global : System.Web.HttpApplication
{
// The key to use in the rest of the web site to retrieve the list
public const string ListItemKey = "MyListItemKey";
// a class to hold your actual values. This can be use with databinding
public class NameValuePair
{
public string Name{get;set;}
public string Value{get;set;}
public NameValuePair(string Name, string Value)
{
this.Name = Name;
this.Value = Value;
}
}

protected void Application_Start(object sender, EventArgs e)
{
InitializeApplicationVariables();
}


protected void InitializeApplicationVariables()
{
List<NameValuePair> listItems = new List<NameValuePair>();
// replace the following code with your data access code and fill in the collection
listItems.Add( new NameValuePair("Item1", "1"));
listItems.Add( new NameValuePair("Item2", "2"));
listItems.Add( new NameValuePair("Item3", "3"));
// load it in the application object
Application[ListItemKey] = listItems;
}
}


现在,您可以在项目的其余部分中访问列表。例如,在default.aspx中将值加载到DropDownList中:

<asp:DropDownList runat="server" ID="ddList" DataTextField="Name" DataValueField="Value"></asp:DropDownList>


并在代码隐藏文件中:

protected override void OnPreInit(EventArgs e)
{
ddList.DataSource = Application[Global.ListItemKey];
ddList.DataBind();
base.OnPreInit(e);
}

关于caching - 在.net Web应用程序中加载高度重复使用的数据的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com