ASP.NET MVC Global Variables

ASP.NET MVC Global Variables

public static class GlobalVariables
{
// readonly variable
public static string Foo
{
get
{
return "foo";
}
}

// read-write variable
public static string Bar
{
get
{
return HttpContext.Current.Application["Bar"] as string;
}
set
{
HttpContext.Current.Application["Bar"] = value;
}
}
}

Global variable declaration in mvc

You can implement it through a static class just like in a traditional c# assembly:

public static class Globals
{
public const Int32 BUFFER_SIZE = 10; // Unmodifiable
public static String FILE_NAME = "Output.txt"; // Modifiable
public static readonly String CODE_PREFIX = "US-"; // Unmodifiable
}

Please, refer to this old answer of mine for more information:

https://stackoverflow.com/a/14368428/796085

How to set a global variable inside c# method

I don't think you can define global variables, but you can have static members.

public static class MyStaticValues
{
public static string currentUrl{get;set;}
}

And then you can retrieve that from anywhere in the code:

String SiteName = MyStaticValues.currentUrl + value.ToString();

Use global variable in ASP.NET MVC

Here is an example using a base controller approach:

public abstract class BaseController : Controller
{
protected string ConnectionString = WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

protected void LogException(Exception ex)
{
var path = HttpContext.Server.MapPath("~/App_Data");
ExceptionLog.Create(Ex, path);
}
}

public class DashboardController : BaseController
{
public ActionResult Index()
{
try
{
string conn = base.ConnectionString;
//codes
}
catch (Exception ex)
{
base.LogException(ex);
}
return View("AdminDashboard");
}
}

Use static global variable class in ASP.NET MVC web application

To answer your question as to whether it is 'okay' to do this, I think that comes down to you.

I think the biggest thing to know is when that data is going to get refreshed. From experience I believe that static information gets stored in the application pool, so if it is restarted then the information will be refreshed.

Lifetime of ASP.NET Static Variable

Consider how many times you need that information, if you only need it once at startup, is it worth having it as a static. If you are getting that information a lot (and say for example it is stored in a database) then it may be sensible to store that in a cache somewhere such as a static member.

I think my only recommendation with static member variables is asp is keep them simple, booleans seem fine to me. Remember that users do share the same application meaning that static variables are global for all users. If you want a user specific variable then you want to use sessions cache.

Always remember the two hardest thing in programming

  1. Naming things
  2. Cache invalidation
  3. Off by one errors

https://martinfowler.com/bliki/TwoHardThings.html

Even though this is a joke, it holds a lot of truth

Hope this helps

ASP.NET MVC C# global variable

Can also use session like this:

Session["MyKey"] = "MyValue";

and retrieving like this:

var myVar = (string)Session["MyKey"];

if that's per user value.

Hope this is of help.

How to create a global variable in ASP.NET Core Web API application?

First of all thank you all for finding some time to help me. I took @pm100's solution and made a slight modification to suit my purpose. I could not use it directly as I am using SonarLint and it was showing some issues. So I used a KeyValuePair instead of Dictionary like this:

public static class GlobalData
{
public static KeyValuePair<string,object> Application { get; set; }
}

and I used it in my code like:

string value = GlobalData.Application.Value

and luckily it works without any issue.

Global Variables MVC application

Yes, you can use session, which is global to the current user only:

HttpContext.Current.Session["Login"] = "John Doe";

However, you won't do that in global.asax because Session is a module that gets initialized at a very specific time (see this). As such, you most likely want to do it in the controller at an appropriate time:

public ActionResult Index()
{
this.HttpContext.Session["Login"] = "X";
}

As an oversimplified example.

Best way to add a class with global variables that doesn't change value in ASP.Net MVC

Create one BaseController and define required variable as public constant/read only, then inherit this BaseController to your Controller, you can access that variable.

May be no required to add class.

BaseController

public class BaseController : Controller
{
public const string actvStat = "Active";
public const string inctvStat = "Inactive";

public ActionResult Index()
{
return View();
}
}

public class Variables
{
public const string actvStat = "Active";
public const string inctvStat = "Inactive";
}

HomeController

public class HomeController : BaseController
{
public ActionResult Index()
{
var classItem1 = Variables.actvStat;
var item1 = actvStat;
return View();
}
}


Related Topics



Leave a reply



Submit