在哪里声明(应用程序变量)以及如何在控制器中访问应用程序变量?
如何从application_start()中的数据库中获取model(model.tt文件)值?
我对应用程序变量没有任何想法,所以,如果您对此有所了解,请帮助我。
谢谢..!

最佳答案

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

 private readonly ISystemConfigurationService _systemConfigurationService;


然后,创建构造函数

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


应用启动时获取模型数据

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);
    }


在控制器中,您必须执行此操作

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


然后将其传递以使用查看袋查看。

10-07 13:24
查看更多