C# 如何创建全局变量、ASP.NET、全局asax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1797568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Global variables, ASP.NET, global asax
提问by Shantanu Gupta
I have some data layer classes that are to be used very frequently almost on the whole site.
我有一些数据层类,它们几乎在整个站点上都非常频繁地使用。
I was working on a windows application previously and i used to create its object in module (vb.net) but now i m working in C# and on ASP.NET.
我以前在 Windows 应用程序上工作,我曾经在模块 (vb.net) 中创建它的对象,但现在我在 C# 和 ASP.NET 上工作。
Now i need to do same thing so that i need not create same object multiple times on every page.
I want to use something like using a global variable.
现在我需要做同样的事情,这样我就不需要在每个页面上多次创建相同的对象。
我想使用诸如使用全局变量之类的东西。
How can i do this?
Is it done by using global.asax
can i create my objects in global.asax
我怎样才能做到这一点?
是通过使用 global.asax 完成的,
我可以在 global.asax 中创建我的对象吗
I am a new to asp.net, so try to give the syntax along with an explanation.
我是asp.net 的新手,所以尝试给出语法和解释。
采纳答案by Bob
You don't actually need to use the global.asax. You can make a class that exposes your objects as static
s. This is probably the most simple way
您实际上并不需要使用 global.asax。您可以创建一个将对象公开为static
s 的类。这可能是最简单的方法
public static class GlobalVariables {
public static int GlobalCounter { get; set; }
}
You can also use the Application Stateor even the ASP.NET Cachebecause those are shared across all sessions.
您还可以使用应用程序状态甚至ASP.NET 缓存,因为它们在所有会话之间共享。
However, If I were in this situation, I would use a framework like Spring.NETto manage all my Sington instances.
但是,如果我处于这种情况,我会使用像Spring.NET这样的框架来管理我所有的 Sington 实例。
Here is a quick example of how you would get at your class instances using Spring.NET
这是一个关于如何使用 Spring.NET 获取类实例的快速示例
//The context object holds references to all of your objects
//You can wrap this up in a helper method
IApplicationContext ctx = ContextRegistry.GetContext();
//Get a global object from the context. The context knows about "MyGlobal"
//through a configuration file
var global = (MyClass)ctx.GetObject("MyGloblal");
//in a different page you can access the instance the same way
//as long as you have specified Singleton in your configuration
But really, the bigger question here is why do you need to use global variables? I am guessing you don't really need them and there might be a better big picture soluion for you.
但实际上,这里更大的问题是为什么需要使用全局变量?我猜你真的不需要它们,可能有更好的大局解决方案适合你。
回答by XpiritO
I would recomend you to use application statefor this purpose.
我建议您为此目的使用应用程序状态。
回答by JB King
"ASP.NET Application State Overview" contains an object that can be used to store data across all users, similar to the Session object in terms of being able to store various key-value pairs.
《ASP.NET 应用程序状态概览》包含一个可用于跨所有用户存储数据的对象,在能够存储各种键值对方面类似于 Session 对象。
回答by Graham
I'll skip over the "should" part about using global variables in .NET and show some code I'm working in now that uses Global.asaxfor some "global" variables. Here's some info from that file:
我将跳过关于在 .NET 中使用全局变量的“应该”部分,并展示我现在正在使用的一些代码,这些代码将Global.asax用于一些“全局”变量。这是该文件中的一些信息:
public class Global : System.Web.HttpApplication
{
public enum InvestigationRole
{
Complainent,
Respondent,
Witness,
}
public static string Dog = "Boston Terrier";
}
So from the ASPX pages, you can access those members by opening up the static Global class like so:
因此,从 ASPX 页面,您可以通过打开静态 Global 类来访问这些成员,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
string theDog = Global.Dog;
// copies "Boston Terrier" into the new string.
Global.InvestigationRole thisRole = Global.InvestigationRole.Witness;
// new instance of this enum.
}
Buyer beware though. There are better ways of treating the concept of "global variables" in the .NET world, but the above will at least get you a layer of abstraction above repeating the same strings in all your ASPX pages.
不过买家要小心。在 .NET 世界中有更好的方法来处理“全局变量”的概念,但是上面的方法至少可以让您在所有 ASPX 页面中重复相同的字符串之上获得一个抽象层。
回答by OrizG
Use public structs. They are more efficient than classes and more flexible than enums.
使用公共结构。它们比类更有效,比枚举更灵活。
Create a file (preferably in the '/Classes' folder) with the following code:
使用以下代码创建一个文件(最好在“/Classes”文件夹中):
public struct CreditCardReasonCodes
{
public const int Accepted = 100;
public const int InsufficientFunds = 204;
public const int ExpiredCard = 202;
}
IMPORTANT: Do not put any namespace so that the structure will be seen globally across your Web Application.
重要提示:不要放置任何命名空间,以便在您的 Web 应用程序中全局看到该结构。
To reference it across your code simply use the following syntax:
要在您的代码中引用它,只需使用以下语法:
if (webServiceResult == CreditCardReasonCodes.Accepted)
{
Console.WriteLine("Authorization approved.")
}
Using "const" members also makes your values immutable at compile time with no possibility to have them modified during the execution of the application.
使用“const”成员还可以使您的值在编译时不可变,并且不可能在应用程序执行期间修改它们。
回答by Robert
What's more, I strongly recommend you reading the great article https://lowleveldesign.org/2011/07/20/global-asax-in-asp-net/to understand how the Global class in global.asax works.
此外,我强烈建议您阅读这篇很棒的文章https://lowleveldesign.org/2011/07/20/global-asax-in-asp-net/以了解 global.asax 中的 Global 类是如何工作的。