什么是应用变量?如何在ASP.NET MVC中声明应用程序变量?

时间:2022-07-03 06:26:18

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..!

声明(应用程序变量)和如何在控制器中访问应用程序变量?如何从application_start()中的数据库获取model(model.tt文件)值?我对应用变量一无所知,所以如果你对它有所了解,请帮助我。谢谢..!

1 个解决方案

#1


0  

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

在global.asax文件中首先声明服务,在其中编写linq语法或逻辑,

 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.

然后使用视图包将其传递给视图。

#1


0  

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

在global.asax文件中首先声明服务,在其中编写linq语法或逻辑,

 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.

然后使用视图包将其传递给视图。