Tips
Update Time: January 20, 2016. Startup class
In ASP.net Core 1.0, the Startup class is the entry point for an application, and we can configure different content for different environments.
The compiler looks for all *.cs files under the project folder to compile, and the runtime looks for a class named startup in all namespaces.
Annotations
You can select files and folders that you want (or do not need) to compile by setting the Project.json file, or you can set up search for the Startup class in different assemblies.
The Startup class must define a Configure method, or it can define a configureservices method at the same time. constructors for the Startup class
constructor, you can help us set the location of the configuration file, such as the following code set Appsettings.json.
Public Startup (ihostingenvironment env)
{
//Set configuration file location
var builder = new Configurationbuilder ()
. Addjsonfile ("Appsettings.json")
. Addjsonfile ($ "appsettings.{ Env. Environmentname}.json ", optional:true);
if (env). Isdevelopment ())
{
//For more details on using the user secret Store-http://go.microsoft.com/fwlink/? linkid=532709
Builder. Addusersecrets ();
}
Builder. Addenvironmentvariables ();
Configuration = Builder. Build ();
}
The default Appsettings.json content is as follows:
{
"Logging": {"
includescopes": false,
"LogLevel": {
"Default": "Verbose",
"System": " Information ",
Microsoft:" Information "}}
}
Configureservices Method
Configureservices is used to define which services we use, such as MVC, EF, Identity, Logging, Route, or to customize some services. The services defined here are based on dependency injection (Dependency injection). In ASP.net Core 1.0, the dependency injection technology is heavily used.
In the following example, EntityFramework is configured (for data access, the connection string needs to be set correctly in Appsettings.json), identity (for authentication/logon), and MVC.
public void Configureservices (iservicecollection services) {//ADD framework services. Services. Addentityframework (). Addsqlserver (). adddbcontext<applicationdbcontext> (Options => options.
Usesqlserver (configuration["Data:DefaultConnection:ConnectionString")); Services. Addidentity<applicationuser, Identityrole> (). Addentityframeworkstores<applicationdbcontext> ().
Adddefaulttokenproviders (); Services.
Addmvc ();
ADD application Services. Services.
Addtransient<iemailsender, authmessagesender> (); Services.
Addtransient<ismssender, authmessagesender> (); }
After Configureservices is invoked, the runtime invokes the Configure method. Configure Method
The Configure method is used to load various middleware requirements, similar to the traditional Owin (Open Web Interface for. NET). The Configure method signature must contain Iapplicationbuilder parameters, or some other five-fu ihostingenvironment and iloggerfactory parameters.
public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {//output log in console
Loggerfactory.addconsole (Configuration.getsection ("Logging"));
Loggerfactory.adddebug (); In the development environment, displays the error details if (env). Isdevelopment ()) {app.
Usebrowserlink (); App.
Usedeveloperexceptionpage (); App.
Usedatabaseerrorpage (); else {//Otherwise, guide the bug page app.
Useexceptionhandler ("/home/error"); Create database try {using (var Servicescope = app. Applicationservices.getrequiredservice<iservicescopefactory> ().
Createscope ()) {servicescope.serviceprovider.getservice<applicationdbcontext> () .
Database.migrate (); The catch {}} app. Useiisplatformhandler (Options => options.
Authenticationdescriptions.clear ()); Allows access to the static file app under the Wwwroot folder.
Usestaticfiles (); Set up an identity testCard mode app.
Useidentity (); Sets the MVC routing app. USEMVC (routes => {routes.
Maproute (name: "Default", Template: "{controller=home}/{action=index}/{id?}");
});
}
Warning
The route to set up MVC must be set in Configure, and the setting in Configureservices is not valid.