For non-static variables, I sorted them using the Application Class Dictionary , as shown below:
In Global.asax.ac:
namespace MvcWebApplication { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { private string _licensefile; // the global private variable internal string LicenseFile // the global controlled variable { get { if (String.IsNullOrEmpty(_licensefile)) { string tempMylFile = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(LDLL.License)).Location), "License.l"); if (!File.Exists(tempMylFile)) File.Copy(Server.MapPath("~/Content/license/License.l"), tempMylFile, true); _licensefile = tempMylFile; } return _licensefile; } } protected void Application_Start() { Application["LicenseFile"] = LicenseFile;// the global variable bed AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } } }
And in the controller:
namespace MvcWebApplication.Controllers { public class HomeController : Controller {
That way we can have global variables in ASP.NET MVC :)
NOTE. If your object is not a string, simply write:
return View(HttpContext.Application["X"] as yourType);
Yasser Zamani May 21 '12 at 7:25 a.m. 2012-05-21 07:25
source share