简体   繁体   中英

what is application variable? How do i declare Application variables in ASP.NET MVC?

Where to declare(Application variable) and How to access application variable in controller ?
How to get model(model.tt file) value from database in application_start() ? i don't have any idea about application variable, So if you know anything about it then help me. Thanks..!

In global.asax file first declare service in which you write linq syntax or your logic,

 private readonly ISystemConfigurationService _systemConfigurationService;

Then, create constructor

public MvcApplication()
    {
        _systemConfigurationService = new SystemConfigurationService();
    }

Get Model Data when app start

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        List<SystemConfigurationModel> systemConfigurationValue = General.MapList<System_Configuration , SystemConfigurationModel> (_systemConfigurationService.GetAllSystemConfigData());
        Application["SystemConfig"] = new List<SystemConfigurationModel>(systemConfigurationValue);
    }

In controller you have to do this,

List<SystemConfigurationModel> applicationState = HttpContext.Application["SystemConfig"] as List<SystemConfigurationModel>;
ViewBag.ContactEmail = applicationState.Find(x => x.Config_Key == "ContactMail").Value;

Then Pass it to view using view bag.

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