将 MVC Application_Start 中的一些值分配给静态 class

Assign some values from MVC Application_Start to a static class

我需要在我的 MVC 应用程序的布局视图中显示一些数据库信息。

我在想,如果我在 Application_Start() 方法中这样做,并分配给具有 静态值 的静态 class,视图将能够访问这些变量并显示它们...但是它们显示为空白。

这里是 Application_Start():

NWatchEntityApplication nWatchApp;

protected void Application_Start()
{
    var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    nWatchApp = new NWatchEntityApplication(GetNWatchConfig());
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(nWatchApp.Configuration.NWatchDatabase);
    Infrastructure.ApplicationInfo.NWatchDatabaseCatalog = builder.InitialCatalog;
    Infrastructure.ApplicationInfo.NWatchDatabaseServer = builder.DataSource;

    var context = nWatchApp.GetDbContext();
    builder = new SqlConnectionStringBuilder(context.DatabaseConnectionString);
    Infrastructure.ApplicationInfo.EntityDatabaseCatalog = builder.InitialCatalog;
    Infrastructure.ApplicationInfo.EntityDatabaseServer = builder.DataSource;

    var webApiContainer = new Container();
    webApiContainer.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
    RegisterTypes(webApiContainer);
    webApiContainer.RegisterWebApiControllers(GlobalConfiguration.Configuration);
    webApiContainer.Verify();

    GlobalConfiguration.Configuration.DependencyResolver =
        new SimpleInjectorWebApiDependencyResolver(webApiContainer);
}

这是保存信息的静态 class:

public static class ApplicationInfo
{
    public static string NWatchDatabaseServer { get; set; }

    public static string NWatchDatabaseCatalog { get; set; }

    public static string EntityDatabaseServer { get; set; }

    public static string EntityDatabaseCatalog { get; set; }
}

下面是应该能够访问这些变量的视图部分:

<div class="footer">
    <div class="footer-inner">
        <div class="footer-content">
            <p>@BranchCircuits_Web.Infrastructure.ApplicationInfo.EntityDatabaseServer
            \@BranchCircuits_Web.Infrastructure.ApplicationInfo.EntityDatabaseCatalog</p>
        </div>
    </div>
</div>

有谁知道为什么这些在视图呈现时会显示为空白?

只有在应用程序启动时才能执行此类操作的合适位置在哪里?

Application_Start 方法中填充静态 class 可能不是最好的主意。其一,它减慢了应用程序的初始启动时间。此外,使用这种方法,您不能进行惰性实例化并等到对该对象的第一个请求发生时再创建它。这意味着您可以创建该实例,并且对您网站的任何实例或该值的第一个请求要到很长一段时间后才会发生。

这是静态 class 的 Lazy<T> 实现。此 class 及其相应的代码可以 存在于 项目的任何部分,并且可以 项目的任何部分引用。使用 Lazy<T> 将确保对象仅在调用代码中的某处使用时才被实例化。

public class ApplicationInfo
{
    private static readonly Lazy<ApplicationInfo> _instance = new Lazy<ApplicationInfo>(() => new ApplicationInfo());

    public static ApplicationInfo Instance
    {
        get { return _instance.Value; }
    }

    public string NWatchDatabaseServer { get; set; }

    public string NWatchDatabaseCatalog { get; set; }

    public string EntityDatabaseServer { get; set; }

    public string EntityDatabaseCatalog { get; set; }

    private ApplicationInfo()
    {
        //ASSIGN VALUES HERE
        NWatchEntityApplication nWatchApp = new NWatchEntityApplication(GetNWatchConfig());
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(nWatchApp.Configuration.NWatchDatabase);
        Infrastructure.ApplicationInfo.NWatchDatabaseCatalog = builder.InitialCatalog;
        Infrastructure.ApplicationInfo.NWatchDatabaseServer = builder.DataSource;

        var context = nWatchApp.GetDbContext();
        builder = new SqlConnectionStringBuilder(context.DatabaseConnectionString);
        Infrastructure.ApplicationInfo.EntityDatabaseCatalog = builder.InitialCatalog;
        Infrastructure.ApplicationInfo.EntityDatabaseServer = builder.DataSource;

    }
}

假设 class 存在于项目的根目录中,即没有命名空间,您可以从如下视图访问该对象:

@ApplicationInfo.Instance.EntityDatabaseServer

如果您有任何问题,请告诉我。

如果您不介意,您应该使用普通的(可实例化的)ApplicationInfo class,如删除 static 一样。然后在(静态)实用程序 class 中,说 Utility,添加类似于以下内容的静态 属性:

public static ApplicationInfo AppInfo
{
    get
    {
        return HttpRuntime.Cache["ApplicationInfo"] as ApplicationInfo; //using System.Web;
        //Or, if you prefer this: 
        //MemoryCache.Default["ApplicationInfo"] as ApplicationInfo; //using System.Runtime.Caching;
    }
    set
    {
        HttpRuntime.Cache["ApplicationInfo"] = value; //using System.Web;
        //Or, if you prefer this: 
        //MemoryCache.Default["ApplicationInfo"] = value; //using System.Runtime.Caching;
    }
}

设置在Application_Start:

var appInfo = new Infrastructure.ApplicationInfo();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(nWatchApp.Configuration.NWatchDatabase);
appInfo.NWatchDatabaseCatalog = builder.InitialCatalog;
appInfo.NWatchDatabaseServer = builder.DataSource;

var context = nWatchApp.GetDbContext();
builder = new SqlConnectionStringBuilder(context.DatabaseConnectionString);
appInfo.EntityDatabaseCatalog = builder.InitialCatalog;
appInfo.EntityDatabaseServer = builder.DataSource;

// Store 'appInfo' in RAM
Infrastructure.Utility.AppInfo = appInfo;

然后你的div就会变成

<div class="footer">
    <div class="footer-inner">
        <div class="footer-content">
            <p>@BranchCircuits_Web.Infrastructure.Utility.AppInfo.EntityDatabaseServer
        \@BranchCircuits_Web.Infrastructure.Utility.AppInfo.EntityDatabaseCatalog</p>
        </div>
    </div>
</div>