Developing an ASP. NET Core Webapi with visual Studio Code learning Notes (i)--start

Source: Internet
Author: User
Tags dotnet hosting


This document documents the entire process of installing the visual Studio Code development tools, the. Net Core 1.0 SDK, and the development of a simple Web-demo Web site in a Windows environment.

First, install visual Studio Code


Installation file: VS Code, the current version is 1.3.



It is recommended to install the latest version, because the debug plugin is included to support breakpoint debugging on vs code.





Second, install the. Net Core 1.0 SDK


installation files:. Net Core SDK





Third, create a. Net Core application


1. Open the cmd window and create a directory as the project directory






2. Enter the directory and use the following three commands to initialize a. Net core application and run


dotnet NewDotNet Restoredotnet Run


The results of the operation are as follows. When Hello world! is present, the application has been initialized successfully.








Iv. writing a simple Web-demo program using visual Studio code


1. Use vs code to open the folder created in the previous two steps






2. Open the Project.json file and modify the contents to the following code snippet


 
 
 1 {
 2   "version": "1.0.0-*",
 3   "buildOptions": {
 4     "debugType": "portable",
 5     "emitEntryPoint": true
 6   },
 7   "dependencies": {
 8     "Microsoft.NETCore.App": {
 9       "type": "platform",
10       "version": "1.0.0"
11     },
12     "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
13   },
14   "frameworks": {
15     "netcoreapp1.0": {
16       "imports": "dnxcore50"
17     }
18   }
19 }


3. Execute the dotnet Restore command in the cmd window to update the NuGet package






4. Create the Startup.cs file and write the following:


 
 1 using Microsoft.AspNetCore.Builder;
 2 using Microsoft.AspNetCore.Hosting;
 3 using Microsoft.AspNetCore.Http;
 4 
 5 namespace WebApiFrame
 6 {
 7     public class Startup
 8     {
 9         public void Configure(IApplicationBuilder app)
10         {
11             app.Run(context =>
12             {
13                 return context.Response.WriteAsync("Hello World!");
14             });
15         }
16     }
17 }


5. Open the Program.cs file and modify the contents to the following code snippet


 
using Microsoft.AspNetCore.Hosting;

namespace WebApiFrame
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}




Five, run the debug Web-demo application


1. Switch to the Debug window and initialize the Debug configuration file



Initializing the Launch.json configuration file









Modify the contents of a Launch.json file






Initializing the Tasks.json configuration file



Initial start debugging, need to configure task run program, generate Tasks.json configuration file














Six, start debugging


Start debugging again, the program will run normally, you can see the log output in the Debug console






Access the default path: http://localhost:5000, you can see the page display Hello world!









To this, a simple Web-demo application is completed.





Explain:

1. The Microsoft.AspNetCore.Server.Kestrel package encapsulates a lightweight HTTP Server called Kestrel, which enables the Web application to run out of IIS for deployment.


Developing an ASP. NET Core Webapi with visual Studio Code learning Notes (i)--start


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.