我需要在我的 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>
有谁知道为什么在视图渲染时这些会显示为空白?
只有在应用程序启动时才能执行此类操作的合适位置在哪里?