Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variables in ASP.Net Core 2

I am developing a web application in ASP.NET Core and currently have a large set of keys, such as stripe account keys. Instead of having them spread throughout the project in different classes I would like to place them all together in json where they could be accessed globally. I have tried placing them in appsettings.json but cannot access them anywhere.

like image 397
Luke Davidson Avatar asked Oct 04 '18 20:10

Luke Davidson


People also ask

How can use global variable in ASP NET?

The most common way of accessing global variables in ASP.net are by using Application, Cache, and Session Objects. Application – Application objects are application level global variables, that need to be shared for all user sessions. Thus, data specific to a user should'nt be saved in application objects.

How do I declare a global variable in C#?

However, you can make a global variable by creating a static class in a separate class file in your application. First, create a class called global in your application with the code given below. The class also has a static variable. Now, you can access it from anywhere in your forms after assigning a value to it.

Are there global variables in C#?

C# is an object-oriented programming (OOP) language and does not support global variables directly. The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances.

How do I create a global variable in razor view?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.


2 Answers

I often do this kind of thing with connection strings and other global constants. First create a class for those variables that you need. In my project it is MDUOptions but whatever you want.

public class MDUOptions
{
    public string mduConnectionString { get; set; }
    public string secondaryConnectionString { get; set; }
}

Now in your Startup.cs ConfigureServices method:

Action<MDU.MDUOptions> mduOptions = (opt =>
{
    opt.mduConnectionString = Configuration["ConnectionStrings:mduConnection"];
});
services.Configure(mduOptions);
services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<MDUOptions>>().Value);

Now you use DI to access it in code:

public class PropertySalesRepository : IPropertySalesRepository
{
    private static string _mduDb;

    public PropertySalesRepository(MDUOptions options)
    {
        _mduDb = options.mduConnectionString;
    }
    ....
}

In my case the only property I wanted was the string but I could have used the entire options class.

like image 55
nurdyguy Avatar answered Sep 16 '22 18:09

nurdyguy


In appsettings.json keep the variables.

{
    "foo": "value1",
    "bar": "value2",
}

Create AppSettings class.

public class AppSettings
{
    public string foo { get; set; }

    public string bar { get; set; }
}

In Startup.cs file register.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration);
}

Usage,

public class MyController : Controller
{
    private readonly IOptions<AppSettings> _appSettings;

    public MyController(IOptions<AppSettings> appSettings)
    {
        _appSettings = appSettings;
    }
    var fooValue = _appSettings.Value.foo;
    var barValue = _appSettings.Value.bar;
}
like image 41
prisar Avatar answered Sep 18 '22 18:09

prisar