asp.net Core 1.0 Introductory--application startup__.net

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.