10

Click here to load reader

Web APIs mit ASP.NET MVC Core 1

Embed Size (px)

Citation preview

Page 1: Web APIs mit ASP.NET MVC Core 1

1

Web APIs mit ASP.NET MVC Core 1

Manfred Steyer

twitter.com/ManfredSteyer

ManfredSteyer

ROUTING UND WEB APIS

Page 2

Page 2: Web APIs mit ASP.NET MVC Core 1

2

Was ist Routing?

Url Action-Methode

Folie 3

Web APIs in MVC Core 1

Kein eigenes Routing für Web APIs

Selbe Konzept, wie für MVC-Anwendungen

Routing berücksichtigt keine URL-Parameter

Folie 5

Page 3: Web APIs mit ASP.NET MVC Core 1

3

Standard-Route in MVC

Folie 6

// Startup.csapp.UseMvc(routes =>{

routes.MapRoute(name:"default",template:"{controller=Home}/{action=Index}/{id?}");

});

Nutzung der Standard-Route

Folie 7

public class FlugController: Controller{

public Flug GetById(int id) { […] }

public List<Flug> GetByRoute(string von, string nach) { […] }

public List<Flug> GetByDate(DateTime datum) { […] }

public void PostFlug([FromBody] Flug flug) { […] }

}

Page 4: Web APIs mit ASP.NET MVC Core 1

4

Nutzung der Standard-Route

Folie 8

public class FlugController: Controller{

// GET|PUT|POST|… Flug/GetById/{id}public Flug GetById(int id) { […] }

public List<Flug> GetByRoute(string von, string nach) { […] }

public List<Flug> GetByDate(DateTime datum) { […] }

public void PostFlug([FromBody] Flug flug) { […] }

}

Nutzung der Standard-Route

Folie 9

public class FlugController: Controller{

// GET|PUT|POST|… Flug/GetById/{id}public Flug GetById(int id) { […] }

// GET|PUT|POST|… Flug/GetByRoute?von=…&nach=…public List<Flug> GetByRoute(string von, string nach) { […] }

// GET|PUT|POST|… Flug/GetByDate?datum=…public List<Flug> GetByDate(DateTime datum) { […] }

// GET|PUT|POST|… Flug/PostFlugpublic void PostFlug([FromBody] Flug flug) { […] }

}

Page 5: Web APIs mit ASP.NET MVC Core 1

5

Web API mit Attribut-basierten Routen

Folie 10

[Route("api/[controller]")]public class FlugController: Controller{

[HttpGet("{id}")]public Flug GetById(int id) { […] }

[HttpGet("byRoute")]public List<Flug> GetByRoute(string von, string nach) { […] }

[HttpPost]public void PostFlug([FromBody] Flug flug) { […] }

}

Web API mit Attribut-basierten Routen

Folie 11

[Route("api/[controller]")]public class FlugController: Controller{

// GET api/flug/{id}[HttpGet("{id}")]public Flug GetById(int id) { […] }

[HttpGet("byRoute")]public List<Flug> GetByRoute(string von, string nach) { […] }

[HttpPost]public void PostFlug([FromBody] Flug flug) { […] }

}

Page 6: Web APIs mit ASP.NET MVC Core 1

6

Web API mit Attribut-basierten Routen

Folie 12

[Route("api/[controller]")]public class FlugController: Controller{

// GET api/flug/{id}[HttpGet("{id}")]public Flug GetById(int id) { […] }

// GET api/flug/byRoute?von=...&nach=...[HttpGet("byRoute")]public List<Flug> GetByRoute(string von, string nach) { […] }

[HttpPost]public void PostFlug([FromBody] Flug flug) { […] }

}

Web API mit Attribut-basierten Routen

Folie 13

[Route("api/[controller]")]public class FlugController: Controller{

// GET api/flug/{id}[HttpGet("{id}")]public Flug GetById(int id) { […] }

// GET api/flug/byRoute?von=...&nach=...[HttpGet("byRoute")]public List<Flug> GetByRoute(string von, string nach) { […] }

// POST api/flug[HttpPost]public void PostFlug([FromBody] Flug flug) { […] }

}

Page 7: Web APIs mit ASP.NET MVC Core 1

7

Migration?

Microsoft.AspNet.Mvc.WebApiCompatShim

Folie 14

DEMO: ERSTE SCHRITTE

Page 15

Page 8: Web APIs mit ASP.NET MVC Core 1

8

DEMO: HTTP-ANTWORT BEEINFLUSSEN

Page 16

DEMO: (XML-)FORMATTERKONFIGURIEREN

Page 17

Page 9: Web APIs mit ASP.NET MVC Core 1

9

(SELF-)HOSTING

Page 18

Hosting

Folie 19

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

Anfrage

Antwort

Host-Prozess

HTTP

Page 10: Web APIs mit ASP.NET MVC Core 1

10

Fazit

Neuimplementierung ohne Mehrgleisigkeiten

Leichtgewichtig, plattformübergreifend

Self-Hosting

High-Level-APIs: Vertraut

Low-Level-APIs: Komplett überarbeitet

Automatische Migration nicht möglich

Folie 40

[email protected]

SOFTWAREarchitekt.at

ManfredSteyer

Contact