提供ap共用的參數設定
Source:https://blogs.msdn.microsoft.com/mvpawardprogram/2017/06/27/teaching-asp-net-mvc-right/
Configuration Data in Classic ASP.NET MVC
In classic ASP.NET MVC, the start of an application is notified through the Application_Start event you handle in global.asax.
public class StarterKitApplication : HttpApplication
{
public static StarterKitSettings AppSettings { get; private set; }
protected void Application_Start()
{
// Load configuration data
AppSettings = StarterKitSettings.Initialize();
// More things to do here
...
}
...
}
In Application_Start, you can initialize the class to store all global configuration data. In the code snippet above, this class is called StarterKitSettings and it offers a factory method, named Initialize, to read data from whatever store they were kept, most likely the web.config file. Here’s an example that might work as a good starting point for the settings class of nearly any application.
public class StarterKitSettings
{
public string ApplicationTitle { get; private set; }
public static StarterKitSettings Initialize()
{
var title = ConfigurationManager.AppSettings["starterkit:app-title"]
?? String.Empty;
var settings = new StarterKitSettings
{
ApplicationTitle = String.Format("{0}", title)
};
return settings;
}
}
All members of StarterKitSettings are read-only and initialized only once. An instance of the StarterKitSettings class is exposed out of the application main object as a static property. In this way, the data is globally available, and as fast as possible, because of the static nature of the container. It is still bound to the application via the HttpApplication object – the beating heart of ASP.NET applications. Consuming configuration data is as easy as using the following code:
-- for CS
var title = StarterKitApplication.Settings.ApplicationTitle;
-- for HTML
<html>
<head>
<title>
@StarterKitApplication.Settings.ApplicationTitle
</title>
</head>
...
<html>