LoginSignup
7
10

More than 5 years have passed since last update.

ASP.NET Core 1.0がリリースされたので、ASP.NET Coreを実行

Last updated at Posted at 2016-06-28

Getting Started — ASP.NET documentationの素振りです。

実行環境

  • Mac OS X Elcapitan

前提条件

.NET Coreはインストール済みです。
インストールしていなければ、ASP.NET Core 1.0がリリースされたので、Mac上で.NET CoreをHello World!を参考にしてください。

手順

新しいプロジェクトを作成

mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new

package.jsonの編集

"Microsoft.AspNetCore.Server.Kestrel": "1.0.0"を追加します。

package.json
{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
  },
  "frameworks": {
    "dnxcore50": { }
  }
}

自動生成されたpackage.jsonのframeworksdnxcore50です。
元のサイトのサンプルはnetcoreapp1.0です。
netcoreapp1.0に修正しても、動作は変わりません。
気にしなくて大丈夫です。

Kestrelは

ASP.NET vNext で用意されている 3 種類のサーバー - しばやん雑記

クロスプラットフォーム / Node.js で実績のある libuv を利用

したWebサーバーです。

依存ライブラリのインストール

dotnet restore

Startup.csを追加

機械的に次のファイルを追加します。

Startup.cs
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!");
            });
        }
    }
}

あらゆるリクエストに文字列Hello from ASP.NET Core!を返すアプリケーションです。

Program.csを編集

Program.cs
using System;
using Microsoft.AspNetCore.Hosting;

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

            host.Run();
        }
    }
}

元のサイトで強調表示されているのは、usingMain関数の中だけです。
namespaceも修正が必要です。注意してください。

実行

dotnet run

を実行すると

Project aspnetcoreapp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hosting environment: Production
Content root path: /Users/shigerunakajima/aspnetcoreapp/bin/Debug/netcoreapp1.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

と、表示されます。
ブラウザでhttp://localhost:5000を開くと

スクリーンショット 2016-06-28 13.15.39.png

が表示されます。

 参考

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10