从MVC Application_Start中分配一些值给一个静态类

本文关键字:静态类 一个 Application Start 分配 MVC | 更新日期: 2023-09-27 18:07:14

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

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

这是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);
}
下面是保存info的静态类:
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>

有没有人知道为什么这些会显示空白时,视图渲染?

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

从MVC Application_Start中分配一些值给一个静态类

Application_Start方法中填充静态类可能不是最好的主意。首先,它减慢了应用程序的初始启动时间。此外,使用这种方法,您不能延迟实例化,并等到该对象的第一个请求发生时才创建它。这意味着您可能创建了实例,而对于站点的任何实例或该值的第一个请求直到很长一段时间之后才会发生。

这是一个静态类的Lazy<T>实现。该类及其对应的代码可以驻留在项目的的任何部分的中,并且可以从项目的的任何部分引用。使用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;
    }
}

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

@ApplicationInfo.Instance.EntityDatabaseServer
如果您有任何问题请告诉我。

如果您不介意,您应该使用一个正常的(可实例化的)ApplicationInfo类,例如,删除static s。然后在(静态)实用程序类中,比如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>