web apis mit asp.net core 1
TRANSCRIPT
![Page 1: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/1.jpg)
1
Web APIs mit ASP.NET MVC Core 1 (vormals MVC 6)
Manfred Steyer
ManfredSteyer
Über mich …
Manfred Steyer
SOFTWAREarchitekt.at
Trainer & Consultant
Angular
Server-Side .NET
Page 2
![Page 2: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/2.jpg)
2
Ziel
Überblick über die Möglichkeiten für Web APIs
mit ASP.NET MVC Core 1
Folie 3
Didaktik
Folien
Beispiele
Folie 9
![Page 3: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/3.jpg)
3
Inhalt
Warum ASP.NET Core 1?
Bootstrapping
Routing
Konfigurieren
Metadaten via Swagger
Folie 11
.NET Core
Folie 24
[http://www.hanselman.com/]
![Page 4: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/4.jpg)
4
Vorteile
Folie 25
Cross-Plattform
Side-by-Side
Self-HostingF5-Compile-to-Memory
Hosting
Kestrel (X-Plattform, Self-Host)
WebListener (Windows, Self-Host)
IIS Kestrel
Nginx Kestrel
Folie 26
![Page 5: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/5.jpg)
5
Mehrgleisigkeiten
Web API MVC Web Pages
ASP.NET MVC Core 1
Vereinheitlichung von MVC, Web API
und (künftig) Web Pages
Einheitliche Konzepte für Controller, Views,
Dependency-Injection, Routing, Filters etc.
![Page 6: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/6.jpg)
6
Migration
Code muss angepasst werden
Aber: Aktuelle Framework-Versionen werden
weiterhin gewartet
Keine Pläne für Migration von WCF und Web Forms
WCF Web Forms Web API 2MVC 5
.NET 4.x / "Full CLR"
ASP.NET CORE 1:BOOTSTRAPPING
Page 34
![Page 7: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/7.jpg)
7
Middleware-Komponenten
Folie 35
Se
rve
r
We
b-F
ram
ew
ork
We
b-A
pp
lica
tion
Mid
dle
ware
1
Mid
dle
ware
2
Mid
dle
ware
…
Mid
dle
ware
n
Request
Response
Host-Process
HTTP
Pipeline konfigurieren
Folie 36
public class Startup{
[…]
public void Configure(IApplicationBuilder app) {
[…]app.UseStaticFiles();app.UseMvc();[…]
}}
![Page 8: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/8.jpg)
8
Umgebung berücksichtigen
Folie 37
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{[…]
if (env.IsDevelopment()) {app.UseDeveloperExceptionPage();
}else {
app.UseExceptionHandler("/Home/Error");}
[…]}
Services konfigurieren
Folie 38
public class Startup{
public void ConfigureServices(IServiceCollection services){
[…]services.AddMvc()[…]
}
[…]}
![Page 9: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/9.jpg)
9
DEMO
Page 40
WEB APIS MIT MVC CORE 1
Page 50
![Page 10: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/10.jpg)
10
Web APIs in MVC Core 1
Kein eigenes Routing für Web APIs
Selbes Konzept, wie für MVC
Keine Konventionen für HTTP Verb,
wie GetAll() GET, PostData() POST
Routing berücksichtigt keine URL-Parameter. URL
muss eindeutig auf Action-Methode abbildbar sein!
Aber: WebApiCompatShim
Folie 51
Web API mit Attribut-basierten Routen
Folie 52
[Route("api/[controller]")]public class FlightController: Controller{
[HttpGet("{id}")]public Flight GetById(int id) { […] }
[HttpGet("byRoute")]public List<Flight> GetByRoute(string from, string to) { […] }
[HttpPost]public void PostFlight([FromBody] Flight flight) { […] }
}
![Page 11: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/11.jpg)
11
Web API mit Attribut-basierten Routen
Folie 53
[Route("api/[controller]")]public class FlightController: Controller{
// GET api/flight/{id}[HttpGet("{id}")]public Flight GetById(int id) { […] }
[HttpGet("byRoute")]public List<Flight> GetByRoute(string from, string to) { […] }
[HttpPost]public void PostFlight([FromBody] Flight flight) { […] }
}
Web API mit Attribut-basierten Routen
Folie 54
[Route("api/[controller]")]public class FlightController: Controller{
// GET api/flight/{id}[HttpGet("{id}")]public Flight GetById(int id) { […] }
// GET api/flight/byRoute?from=...&to=...[HttpGet("byRoute")]public List<Flight> GetByRoute(string from, string to) { […] }
[HttpPost]public void PostFlight([FromBody] Flight flight) { […] }
}
![Page 12: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/12.jpg)
12
Web API mit Attribut-basierten Routen
Folie 55
[Route("api/[controller]")]public class FlightController: Controller{
// GET api/flight/{id}[HttpGet("{id}")]public Flight GetById(int id) { […] }
// GET api/flight/byRoute?from=...&to=...[HttpGet("byRoute")]public List<Flight> GetByRoute(string from, string to) { […] }
// POST api/flight[HttpPost]public void PostFlight([FromBody] Flight flight) { […] }
}
DEMO
Page 56
![Page 13: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/13.jpg)
13
MVC und Formatter konfigurieren
Folie 57
public class Startup{
public void ConfigureServices(IServiceCollection services){
services.AddMvc().AddJsonOptions(options => { […] }).AddMvcOptions(options => { […] });
}
[…]}
XML-Formatter
Package:
Microsoft.AspNetCore.Mvc.Formatters.Xml
XmlDataContractSerializerInputFormatter
XmlDataContractSerializerOutputFormatter
Folie 58
![Page 14: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/14.jpg)
14
DEMO
Page 59
META-DATEN MIT SWAGGER
Page 60
![Page 15: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/15.jpg)
15
Swagger
JSON-basiertes Metadatenformat für Web APIs
Weit verbreitet
Kein offizieller Standard
Folie 61
Swagger
Folie 62
JSON-Schema für Datentypen
Operationen (Verb, Url,
Datentypen für Anfrage und Antworten)
![Page 16: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/16.jpg)
16
SWASHBUCKLE
Page 63
Swashbuckle
Swagger-Implementierung für
ASP.NET MVC Core 1
Installation via NuGet
Startup.ConfigureServices:
services.AddSwaggerGen();
Startup.Configure
app.UseSwaggerGen(); -- Swagger-Dokument via /swagger
app.UseSwaggerUi(); -- Swaager-UI via /swagger/ui
Folie 64
![Page 17: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/17.jpg)
17
DEMO
Page 65
SECURITY
Page 66
![Page 18: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/18.jpg)
18
Authentifizierung via Benutzer/Passwort
HTTP-BASIC via IIS oder HttpListener
Eigene HTTP-Middleware
Folie 67
Authentication via Tokens
JwtBearerAuthentication-Middleware
Prüft JWT-Tokens
Übernimmt Claims des JWT-Token in User-Objekt
Folie 68
![Page 19: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/19.jpg)
19
OAuth2: Prinzipieller Ablauf
Folie 69
Client
Authorization-Server
Resource-Server
3. Token
Details legt Flow fest Ein zentrales Benutzerkonto
Nur Auth-Svr. kennt Passwort
Auth. von Client entkoppelt
Flexibilität durch Token
SPA: Kein Cookie: Kein CSRF
DEMO
Page 70
![Page 20: Web APIs mit ASP.NET Core 1](https://reader034.vdocuments.site/reader034/viewer/2022052300/5871231d1a28abe4448b5831/html5/thumbnails/20.jpg)
20
Zusammenfassung
Folie 82
X-Plattform Routing Formatter
Swashbuckle Self-Hosting Vereinheitlichung
Middleware
SOFTWAREarchitekt.at
ManfredSteyer
Kontakt