LoginSignup
7
7

More than 5 years have passed since last update.

ASP.NET Core 2.1 の新機能

Last updated at Posted at 2018-06-05

初めに

ASP.NET Core 2.1.0がリリースされました。(2018/5/30)

SPAテンプレートが大きく変わっています。その他、書き直されたSignalRがリリースされています。

私はAPIサーバーとしての利用が主なので、以下は抑えておきたいとメモしています。
* ApiController, ActionResult
* IHttpClientFactory
* Integration tests
* SignalR

新機能一覧

各機能の説明

以下に説明があります。(2018/5/30 更新)
What's new in ASP.NET Core 2.1 | Microsoft Docs

Updated SPA templates

Angular、React、React with ReduxのSingle Page Applicationテンプレートは、各フレームワークの標準プロジェクト構造とビルドシステムを使用するように更新されています。

各フレームワーク(Angular CLIおよびcreate-react-app)用のシステムを構築するように更新されました。
AngularテンプレートはAngular CLIに基づいており、Reactテンプレートはcreate-react-appに基づいています。
Reactの場合にはcreate-react-app で作成されるプロジェクトに準じた構成になりました。Not TypeScrpit!!

詳細は、次を参照してください。
Use the Single Page Application templates with ASP.NET Core.

Integration Tests

テストの作成と実行を合理化する新しいパッケージが導入されました。
NuGet Gallery | Microsoft.AspNetCore.Mvc.Testing 2.1.0

やり方

  • Copies the dependency file (*.deps) from the tested app into the test project's bin folder.
  • Sets the content root to the tested app's project root so that static files and pages/views are found when the tests are executed.
  • Provides the WebApplicationFactory class to streamline bootstrapping the tested app with TestServer.

次のテストでは、xUnitを使用して、インデックスページが成功ステータスコードと正しいContent-Typeヘッダーでロードされていることを確認します。

public class BasicTests
    : IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
    private readonly HttpClient _client;

    public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task GetHomePage()
    {
        // Act
        var response = await _client.GetAsync("/");

        // Assert
        response.EnsureSuccessStatusCode(); // Status Code 200-299
        Assert.Equal("text/html; charset=utf-8",
            response.Content.Headers.ContentType.ToString());
    }
}

詳細については、統合テストのトピックを参照してください。

ApiController, ActionResult

ASP.NET Core 2.1には、新しいプログラミング規約が追加されているため、きれいで記述的なWeb APIを簡単に作成できます。
ActionResult <T>は、レスポンスタイプまたはその他のアクション結果(IActionResultに似ています)のいずれかを返し、応答タイプをまだ示すことができるようにするために追加された新しいタイプです。
[ApiController]属性は、Web API固有の規則と振る舞いにオプトインする方法として追加されました。

詳細については、ASP.NETコアによるWeb APIのビルドを参照してください。

SignalR

SignalRはASP.NET Core 2.1用に書き直されました。 ASP.NET Core SignalRにはいくつかの改善点があります。

簡略化されたスケールアウトモデル。
jQuery依存関係のない新しいJavaScriptクライアント。
MessagePackに基づく新しいコンパクトバイナリプロトコル。
カスタムプロトコルのサポート。
新しいストリーミング応答モデル。
裸のWebSocketに基づくクライアントのサポート。

詳細については、ASP.NET Core SignalRを参照してください。

Razor class libraries

ASP.NET Core 2.1を使用すると、RazorベースのUIを構築してライブラリに組み込むことが容易になり、複数のプロジェクトに共有できます。 新しいRazor SDKを使用すると、RazorファイルをNuGetパッケージにパッケージ化できるクラスライブラリプロジェクトに組み込むことができます。
ライブラリ内のビューとページは自動的に検出され、アプリでオーバーライドできます。 Razorコンパイルをビルドに統合することによって:

アプリの起動時間が大幅に短縮されます。
実行時のレイザービューとページへの高速更新は、反復開発ワークフローの一環として引き続き利用できます。
詳細については、Razorクラスライブラリプロジェクトを使用した再利用可能なUIの作成を参照してください。

Identity UI library & scaffolding

ASP.NET Core 2.1 provides ASP.NET Core Identity as a Razor Class Library. Apps that include Identity can apply the new Identity scaffolder to selectively add the source code contained in the Identity Razor Class Library (RCL). You might want to generate source code so you can modify the code and change the behavior. For example, you could instruct the scaffolder to generate the code used in registration. Generated code takes precedence over the same code in the Identity RCL.

Apps that do not include authentication can apply the Identity scaffolder to add the RCL Identity package. You have the option of selecting Identity code to be generated.

For more information, see Scaffold Identity in ASP.NET Core projects.

HTTPS

ASP.NET Core 2.1には、開発時にHTTPSを使用して、運用環境でHTTPSを簡単に設定できるようにするいくつかの改良が含まれています。 詳細については、HTTPSを適用するを参照してください。

On by default

To facilitate secure website development, HTTPS is now enabled by default. Starting in 2.1, Kestrel listens on https://localhost:5001 when a local development certificate is present. A development certificate is created:
* As part of the .NET Core SDK first-run experience, when you use the SDK for the first time.
* Manually using the new dev-certs tool.
Run dotnet dev-certs https --trust to trust the certificate.

ローカル開発証明書が存在する場合、Kestrelはhttps:// localhost:5001をリッスンします。 開発証明書が作成されます。

HTTPS redirection and enforcement

すべてのHTTPトラフィックをHTTPSにリダイレクトします。 2.1では、構成やバインドされたサーバーポートの存在に基づいてインテリジェントにリダイレクトする特殊なHTTPSリダイレクトミドルウェアが導入されました。

Configuration for production

本番環境では、HTTPSを明示的に構成する必要があります。
2.1では、Kestrel用にHTTPSを設定するためのデフォルト設定スキーマが追加されました。

IHttpClientFactory

ASP.NET Core 2.1 includes a new IHttpClientFactory service that makes it easier to configure and consume instances of HttpClient in apps. HttpClient already has the concept of delegating handlers that could be linked together for outgoing HTTP requests. The factory:

Makes registering of instances of HttpClient per named client more intuitive.
Implements a Polly handler that allows Polly policies to be used for Retry, CircuitBreakers, etc.
For more information, see Initiate HTTP Requests.

Kestrel transport configuration

With the release of ASP.NET Core 2.1, Kestrel's default transport is no longer based on Libuv but instead based on managed sockets. For more information, see Kestrel web server implementation: Transport configuration.

Generic host builder

Generic Host Builder(HostBuilder)が導入されました。 このビルダーは、HTTPリクエスト(メッセージング、バックグラウンドタスクなど)を処理しないアプリケーションに使用できます。

詳細については、.NET汎用ホストを参照してください。

Razor Pages search for Razor assets

In 2.1, Razor Pages search for Razor assets (such as layouts and partials) in the following directories in the listed order:

Current Pages folder.
/Pages/Shared/
/Views/Shared/

Razor Pages in an area

Razor Pages now support areas. To see an example of areas, create a new Razor Pages web app with individual user accounts. A Razor Pages web app with individual user accounts includes /Areas/Identity/Pages.

Migrate from 2.0 to 2.1

2.0 → 2.1への移行方法

See Migrate from ASP.NET Core 2.0 to 2.1.

GDPR

ASP.NET Core provides APIs and templates to help meet some of the EU General Data Protection Regulation (GDPR) requirements. For more information, see GDPR support in ASP.NET Core. A sample app shows how to use and lets you test most of the GDPR extension points and APIs added to the ASP.NET Core 2.1 templates.

EUの一般データ保護規制(GDPR)要件の一部を満たすのに役立つAPIとテンプレートを提供します。

Additional information

変更の一覧については以下を参照
ASP.NET Core 2.1 Release Notes.

7
7
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
7