简体   繁体   中英

declaring a variable globally so that can be reused anywhere in c# mvc

I went through many links but couldn't find my answer.Links such as Global variables for setting configurations ASP.NET MVC , ASP.NET MVC C# global variable , ASP.NET MVC Global Variables and many more all of them suggest some complex methods like using Application_Start or using session variables, singleton pattern. But I don't want to use these, I just want declare a variable in a certain file and access it anywhere I need.

var dataContext = new PetaPoco.Database("MessageEntity");

This line is required anywhere I need to access the database. So I want to declare this dataContext as global variable in a certain different file and access it on my several other controllers. How would I accomplish this. Please help!!

to create global variable:

public static class Global
{
    public volatile static Object GlobalVariable;
}

It can be accessed everywhere from apllication thru Global.GlobalVariable.

but it is bad solution for data access context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM