Login for your services
Digital Diary My Tasks Bookmarks My Q/As
AXARROW HIGHLIGHTS
This article describes a common scenario of Declaring, Building and Accessing Global Objects and Variables across ASP.NET MVC Controllers. Entity Framework Database First approach is assumed.
Recent Articles
VIEW ARTICLE

Declare and Access Global Objects and Variables across ASP.NET MVC Controllers

Author: Suresh Kr Chaudhary
Category: Programming
Submitted On: 7/25/2017
Published On: 7/25/2017
ABSTRACT: This article describes a common scenario of Declaring, Building and Accessing Global Objects and Variables across ASP.NET MVC Controllers. Entity Framework Database First approach is assumed.

Declare and Access Global Objects and Variables across ASP.NET MVC Controllers

Declaring global variables or objects at application level, and accessing them from across various controller methods, is an ideal scenario in ASP.NET MVC programming model. Three steps are required in this process.
The very first step is to declare or make up for the object. Got to the Model folder of your project and add or Define a class file, in the namespace scope of your project. If you have to build the class object from the database schema, in the Entity Framework database-first approach, go to the Models folder, open the .edmx diagram and choose update from the database. Select the table required to be inducted as global object. An example of such an object updated from the database is given below.


namespace MyProject.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class MyGlobal
    {
        public int GlobalID { get; set; }
        public int myFileSizeLimit { get; set; }
        public bool myServiceOff { get; set; }
        public string myServiceOffMsg { get; set; }
        public short myViewPageSize { get; set; }
    }
}

After declaration, the next step is to create a mechanism to fetch the global object, so that it becomes accessible from all methods. For this, just create a public method, that create object, fill up values from database etc., and return the global object. Following code snippet represents an example of this process. The GetGlobal method can be called from any method from the Controller.
It is assumed that you have established all database requirements (Entity Framework DB object) in your Controller. It is also assumed that for placing global values in your database, you have only one record in the designated table.


public MyGlobal GetGlobal()
 {
    MyGlobal myGlobal = (MyGlobal)HttpContext.Application["myGlobal"];
    if (myGlobal == null) // if global object is not there already...
    {
        myGlobal = db.MyGlobals.Find(1);
        if (myGlobal != null)
        {
            ControllerContext.HttpContext.Application["myGlobal"] = axcGlobal;  // Put-In the Global Scope...
            return axcGlobal;
        }
        else
            return null;
    }
    else
        return axcGlobal;
  }
    

The last step is to invoke the method GetGlobal() from any method. You may try calling the method like the one decribed in the example code below.


public ActionResult Index()
{
 MyGlobal myGlobal = GetGlobal();
    if(myGlobal != null)
    {
        ViewBag.myServiceOffMsg = axcGlobal.myServiceOffMsg;
    }
    else
    {
        ViewBag.myServiceOffMsg = "default message ...";
    }

    //...
    // Your code follows from here
    // Pass the ViewBag values to your View
    //...

}


Go to Top
Declare and Access Global Objects and Variables across ASP.NET MVC Controllers

Related Questions...(0) Ask A Question

Ask a Question

Go to Top