LoginSignup
8
8

More than 5 years have passed since last update.

Ubuntu 16.04 LTSでASP.NET Core 1.0開発環境構築

Last updated at Posted at 2016-09-02

各種の解説や入門記事を見ると開発環境構築手順はリンクで済ませているものが多かったので、個人的なメモの意味も込めて作成する。

各種インストール

1. .NET Core

何はなくとも.NET Coreは必要なのでインストールする。

.NET - Powerful Open Source Development

$ sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
$ sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
$ sudo apt-get update

2. Yeoman

ASP.NET Coreプロジェクトテンプレートを生成するためにYeomanをインストールする。

Yeomanはnpmで配布されているので、Node.jsとnpmもインストールする。

1) Node.js

Ubuntu向けのインストールパッケージを使ってインストールする。

nodesource/distributions: NodeSource Node.js Binary Distributions

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install -y nodejs

2) npm

Node.jsと一緒にインストールされるので、最新版に更新する。

02 - Installing Node.js and updating npm | npm Documentation

$ sudo npm install npm -g

3) Yeoman

npmでインストールする。

Getting started with Yeoman | Yeoman

$ sudo npm install -g yo

4) generator-aspnet

YeomanでASP.NET Coreのプロジェクトを生成するため、Bowerと合わせてインストールする。

generator-aspnet

$ sudo npm install -g bower #Bower
$ sudo npm install -g generator-aspnet

ASP.NET Core プロジェクト作成と実行

1. プロジェクト作成

Yeomanを用いてASP.NET Coreプロジェクトを生成する。

1) Yeoman実行

$ yo aspnet

     _-----_     ╭──────────────────────────╮
    |       |    │      Welcome to the      │
    |--(o)--|    │  marvellous ASP.NET Core │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

? What type of application do you want to create? (Use arrow keys)
❯ Empty Web Application
  Console Application
  Web Application
  Web Application Basic [without Membership and Authorization]
  Web API Application
  Class Library
  Unit test project (xUnit.net)

2) 空のWebアプリケーションを選択


     _-----_     ╭──────────────────────────╮
    |       |    │      Welcome to the      │
    |--(o)--|    │  marvellous ASP.NET Core │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

? What type of application do you want to create? Empty Web Application
? What's the name of your ASP.NET application? (EmptyWebApplication)

3) プロジェクト名入力

     _-----_     ╭──────────────────────────╮
    |       |    │      Welcome to the      │
    |--(o)--|    │  marvellous ASP.NET Core │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

? What type of application do you want to create? Empty Web Application
? What's the name of your ASP.NET application? AspNetCoreFirstApp
   create AspNetCoreFirstApp/.gitignore
   create AspNetCoreFirstApp/Program.cs
   create AspNetCoreFirstApp/Startup.cs
   create AspNetCoreFirstApp/project.json
   create AspNetCoreFirstApp/web.config
   create AspNetCoreFirstApp/Dockerfile
   create AspNetCoreFirstApp/Properties/launchSettings.json
   create AspNetCoreFirstApp/README.md


Your project is now created, you can use the following commands to get going
    cd "AspNetCoreFirstApp"
    dotnet restore
    dotnet build (optional, build will also happen when it's run)
    dotnet run

2. アプリ実行

dotnetコマンドを用いてパッケージの復元と実行を行う。

1) パッケージの復元

dotnet restoreコマンドで依存パッケージを復元する。

初回は全パッケージをダウンロードするので時間がかかるが、2回目以降はキャッシュしたものを使うので早い。

$ cd AspNetCoreFirstApp
$ dotnet restore
log  : Restoring packages for /home/takano-s/AspNetCoreFirstApp/project.json...
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in /home/takano-s/AspNetCoreFirstApp/project.json...
log  : Writing lock file to disk. Path: /home/takano-s/AspNetCoreFirstApp/project.lock.json
log  : /home/takano-s/AspNetCoreFirstApp/project.json
log  : Restore completed in 1962ms.

2) 実行

dotnet runコマンドでアプリを実行する。既定ではlocalhostの5000番ポートでアプリをホストする。

$ dotnet run
Project AspNetCoreFirstApp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling AspNetCoreFirstApp for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:03.2461046


Hosting environment: Production
Content root path: /home/takano-s/AspNetCoreFirstApp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

3. 動作確認

curl等でhttp://localhost:5000にアクセスすると、"Hello World!"が返ってくる。

$ curl http://localhost:5000/
Hello World!
8
8
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
8
8