0%

.NET Core 安裝&開始

參考: https://www.microsoft.com/net/core

Install for Windows

Install .NET Core SDK

使用Visual Studio:
安裝Visual Studio 2015 Update 3, 再下載.NET Core for Visual Studio official MSI Installer
也可以使用Visual Studio Community 2015 (免費)

使用Command Line(很快):
安裝the .NET Core SDK for Windowshttps://go.microsoft.com/fwlink/?LinkID=809122

Initialize

mkdir hwapp
cd hwapp
dotnet new: Create a new C# project
產生Program.csproject.json
第一次執行會幫你裝SDK在C槽C:\Program Files\dotnet

dotnet new -t可以接 Console, Web, Lib, xunittest

Run

dotnet restore: 產生project.lock.json
dotnet run: Compile (dotnet build只有build)

Getting Started : website with ASP.NET Core

ref:https://docs.asp.net/en/latest/getting-started.html

送版型(很快):
dotnet new -t web 產生一堆東西
dotnet restore 安裝套件
dotnet run 完成 還有一堆版型
http://localhost:5000/

空的專案:
dotnet new

project.json加上 "dependencies": {"Microsoft.AspNetCore.Server.Kestrel": "1.0.0"},
下面的dependencies也可以

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

dotnet restore

建立Startup.cs 處理請求邏輯

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

更新Program.cs
namespace要一樣唷
加上

1
2
3
4
5
6
7
8
using Microsoft.AspNetCore.Hosting;
...
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();

dotnet run!
http://localhost:5000/