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項目:
首先將包引用添加到Microsoft.AspNetCore.Mvc.NewtonsoftJson
更新ConfigureServices方法以添加AddNewtonsoftJson()。
.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 mywebcd myweb
dotnet run
運行起來如下圖:
對應組件代碼Counter.razor :
@page "/counter"<h1>Counter</h1>
<p>LineZero</p>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" οnclick="@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共享框架中刪除了對運行時編譯的支持,現在可以通過向應用程序添加軟件包來啟用它。
要啟用運行時編譯:
添加對Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation的包引用
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0-preview3-19153-02" />在Startup.ConfigureServices加入方法AddRazorRuntimeCompilation
services.AddMvc().AddRazorRuntimeCompilation();Worker Service模板
此模板旨在作為運行長時間運行的后臺進程的起點,例如您可以作為Windows服務或Linux守護程序運行。
單頁面應用程序模板的身份驗證
由IdentityServer在后臺提供支持
dotnet new angular -au Individualdotnet run
最終運行起來,可以進行登錄注冊,及對API 的保護。
更多可以查看官方文檔:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0
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
原文地址:https://www.cnblogs.com/linezero/p/aspnetcore3preview.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的ASP.NET Core 3.0预览版体验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信开发必看,使用.Net Core 开
- 下一篇: ASP.NET Core 基于JWT的认