ASP.NET Core 3.0預覽版體驗


目前.NET Core 3.0的版本為.NET Core 3.0 Preview 3,對應ASP.NET Core 3.0 Preview 3。

ASP.NET Core 3.0 之后將不再支持.NET Framework,只運行在.NET Core 上面。

ASP.NET Core 3.0 現在已經出到了第三個預覽版,增加和改進了很多功能。

 

環境准備:

下載最新.NET Core 3.0 Preview 3 SDK, https://dotnet.microsoft.com/download/dotnet-core/3.0。

ASP.NET Core 3.0 需要VS 2019開發,或者使用VS Code,Visual Studio for Mac version 8.0 or later。

Visual Studio 2019 將會在4月2日推出正式版。

 

下面大致列舉一些功能:

Json.NET 不在內置在框架內

如果要將Json.NET支持添加回ASP.NET Core 3.0項目:

services.AddMvc()
  .AddNewtonsoftJson();

 

HostBuilder 替換掉WebHostBuilder

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

 

UseRouting 中間件的增加

示例代碼:

    app.UseRouting(routes =>
    {
        routes.MapGet("/hello", context =>
        {
            return context.Response.WriteAsync("Hi there! linezero");
        });
    });

同時還增加 MapHealthChecks及RequireHost 等功能,看示例:

app.UseRouting(routes =>
{
    routes.MapGet("/", context => context.Response.WriteAsync("Hi linezero!"))
        .RequireHost("linezero.com");    
    routes.MapGet(context => context.Response.WriteAsync("Hi zero!"))
        .RequireHost("zero.com");

    routes.MapHealthChecks("/healthz").RequireHost("*:8080");
});

Razor Components 

razor 組件支持,下面實際看看這個功能點。

dotnet new razorcomponents -o myweb

cd myweb

dotnet run

運行起來如下圖:

對應組件代碼Counter.razor :

@page "/counter"

<h1>Counter</h1>
<p>LineZero</p>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>

@functions {
    int currentCount = 0;

    void IncrementCount()
    {
        currentCount++;
    }
}

 

 你可以直接將組件添加到主頁或其他頁面,例如放到主頁Index.razor:

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.LineZero

<Counter />

 還可以使用 [Parameter] int IncrementSize { get; set; } = 1; 來設置參數:

@functions {
    int currentCount = 0;

    [Parameter] int IncrementSize { get; set; } = 1;

    void IncrementCount()
    {
        currentCount+=IncrementSize;
    }
}

這樣可以做到每個頁面設置不同的大小,增加不同數量。

如:

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.LineZero

<Counter IncrementSize="6"/>

 

下圖描述了Razor的一些原理。

 

另外,Blazor是一個實驗性單頁面應用程序框架,它使用基於WebAssembly的.NET運行時直接在瀏覽器中運行Razor Components。

在Blazor應用程序中,Razor組件的UI更新都直接應用於DOM

 

運行時編譯

從.NET Core 3.0中的ASP.NET Core共享框架中刪除了對運行時編譯的支持,現在可以通過向應用程序添加軟件包來啟用它。

要啟用運行時編譯:

  1. 添加對Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation的包引用

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0-preview3-19153-02" />
  2. Startup.ConfigureServices加入方法AddRazorRuntimeCompilation

    services.AddMvc().AddRazorRuntimeCompilation();

Worker Service模板

此模板旨在作為運行長時間運行的后台進程的起點,例如您可以作為Windows服務或Linux守護程序運行。

單頁面應用程序模板的身份驗證

由IdentityServer在后台提供支持

dotnet new angular -au Individual

dotnet run

最終運行起來,可以進行登錄注冊,及對API 的保護。

更多可以查看官方文檔:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0

gRPC服務模板

dotnet new grpc

會生成兩個項目,一個在ASP.NET Core中托管的gRPC服務,以及一個用它來測試它的控制台應用程序。

這是gRPC for ASP.NET Core的第一次公開預覽,並沒有實現gRPC的所有功能。對應開源項目: https://github.com/grpc/grpc-dotnet

gRPC 簡單介紹可以參照之前文章:http://www.cnblogs.com/linezero/p/grpc.html  及 https://www.cnblogs.com/linezero/p/grpcnetcore.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM