23
Blazor a novinky v ASP.NET Core 3.0 @RobertHaken Software & Cloud Architect | Microsoft MVP: Development | HAVIT, s.r.o.

Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Blazora novinkyv ASP.NET Core 3.0

@RobertHakenSoftware & Cloud Architect| Microsoft MVP: Development | HAVIT, s.r.o.

Page 2: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

https://askme.blazor.cz

Page 3: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Blazor

Client-side hosting model Server-side hosting model

Page 4: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Supported platforms

Blazor client-side Blazor server-side

Google Chrome (incl. Android) YES YES

Microsoft Edge YES YES

Mozilla Firefox YES YES

Safari (incl. iOS) YES YES

Microsoft Internet Explorer NO v11

https://caniuse.com/#search=webassembly

Page 5: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

BlazorNow

A component model for building composable UI

Routing

Layouts

Forms and validation

Dependency injection

JavaScript interop

IntelliSenseand tooling

Publishing and app size trimming (basic)

Debugging (in browser PoC)

Page 6: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

BlazorPlans

Authentication, Authorization

Live reloading in the browser during development

Server-side rendering

Full .NET debugging both in browsers and in the IDE

Rich IntelliSense and tooling

Publishing and app size trimming

Page 7: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

AskMe – Blazor Edition

Page 8: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Page = Component + Route

Namespace/Namespace/ClassName.razor

@functions => @code

@page "/route"

Page 9: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Layouts

layout

– just another component

– inherits from LayoutComponentBase

– Body property (@Body in markup)

using the layout

– @layout directive (compiles to [LayoutAttribute])

– _Imports.razor

nested layouts

Page 10: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Routing

@pagedirective

@page "/products"

@page "/products/{ProductId} " => [Parameter]ProductId

@page "/products/{ProductId:int}"

App.razor: <RouterAppAssembly="typeof(Program).Assembly"

FallbackComponent="typeof(Pages.NotFound)" />

<NavLinkhref="...“ class="..." Match="NavLinkMatch.All| Prefix" />

IUriHelper.NavigateTo(), .OnLocationChanged, ...

Page 11: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Components

.razor

@functions => @code

parameters -private property + [Parameter]

private RenderFragmentChildContent{ get; set; }

@inherits

@using + _Imports.razor

@inject

@implements

Page 12: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Components Lifecycle methods

OnInit[Async]()

SetParameters(ParameterCollectionparameters)

OnParametersSet[Async]()

bool ShouldRender()

void BuildRenderTree(RenderTreeBuilderbuilder)

OnAfterRender[Async]()

IDisposable.Dispose()

StateHasChanged()

Page 13: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Coded Components

derived from ComponentBase

override BuildRenderTree(RenderTreeBuilderbuilder)

builder

.OpenElement(sequence, elementName); + .CloseElement();

.AddContent(sequence, ...);

.AddMarkupContent(sequence, ...);

.AddAttribute(...)

....

[Inject], [Parameter], [Route], [Layout], ...

Page 14: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Components –Event Handling

on<event>

synchronous + asynchronous

event-args– UIEventArgs

– UIChangeEventArgs

– UIKeyboardEventArgs

– UIMouseEventArgs

– custom

onclick="@(args=> DoSomething(args, itemNumber))“

[Parameter] private EventCallback<TArgs> OnSomething{ get; set; }

Page 15: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Built-in Components

<NavLink/> + <Router /><EditFormModel="@MyModel" OnValidSubmit="@HandleValidSubmit">

<DataAnnotationsValidator/><ValidationSummary/><InputTextid="name" bind-Value="@MyModel.Name" /><ValidationMessageFor="@(() => MyModel.Name)" /><InputTextArea/><InputSelect/><InputNumber/><InputCheckBox/><InputDate/>

</EditForm>+ 3rd party ☺

Page 16: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Data Binding

both Components & DOM elements

<input bind="@MyValue“ /><input value="@MyValue"

onchange="@((UIChangeEventArgs__e) => MyValue= __e.Value)" />

<input type="text" bind-value-oninput="@CurrentValue" />

<input bind="@StartDate" format-value="yyyy-MM-dd" />

<MyComponentbind-MyParameter="@SomeValue" /><MyComponentMyParameter="@SomeValue"

MyParameterChanged="@(v => SomeValue= v)" />

Page 17: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Templated Components

RenderFragment/ RenderFragment<T> parameters

@typeparamTItem

context implicit parameter: @context<ItemTemplateContext="item"> [email protected]... </ItemTemplate>

Page 18: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Razor Templates

@<tag>...</tag>

@{RenderFragmenttemplate = @<p>The time is @DateTime.Now.</p>;RenderFragment<Pet> petTemplate= (pet) => @<p>Name: @pet.Name.</p>

}

@template@petTemplate(new Pet { Name = "Bzuk" })

Page 19: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

JavaScript Interop

IJSRuntime.InvokeAsync<T>(string identifier, params object[] args)

JSON Serializable input + output

window scope

OnAfterRenderAsync

ElementRef

Page 20: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Invoke .NET from JavaScript

DotNet.invokeMethod[Async]('ClassName', 'MethodName')

[JSInvokable]

DotNetObjectRef

Page 21: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

Tips & Tricks

@((MarkupString)myRawHtml)

always set Idfor <InputXy/> components

.csproj: <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>

StateHasChanged()

Debugging

Razor Pages / MVC : Html.RenderComponentAsync<T>(...)

Page 22: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

NovinkyASP.NET Core 3.0

ASP.NET Core 3.0 will only run on .NET CoreMicrosoft.AspNetCore.Appshared fww/o Newtonsoft.Json, EF Core, CodeAnalysis

BlazorServer-Side (Razor Components)

SignalRclient-to-server streaming

Pipes on HttpContext

Generic host in templates

Endpoint routing

Page 23: Software & Cloud Architect| Microsoft MVP: Development | … · 2019-06-19 · BlazorPlans Authentication, Authorization Live reloading in the browser during development Server-side

REFERENCES

Demos https://github.com/hakenr/AskMehttps://github.com/ridercz/AskMe

Blog http://knowledge-base.havit.cz/ + .euwww.blazor.cz

Twitter @RobertHaken

YouTube https://www.youtube.com/user/HAVITcz