0

我需要在我的 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);
}

这是保存信息的静态类:

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>

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

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

4

2 回答 2

1

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

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

请让我知道,如果你有任何问题。

于 2016-09-15T23:14:52.810 回答
0

如果您不介意,您应该使用普通的(可实例化的)ApplicationInfo类,如删除statics. 然后在(静态)实用程序类中,例如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>
于 2016-09-16T05:00:25.663 回答