#

Web API Prüfen

 

Um die Daten der API Schnittstelle zu prüfen, hat man jetzt direkten Zugriff über die URL /Api

Anstatt im Browser die Adresse

localhost:51081/Projects

einzugeben für die http-Webseite im Browser,

 

gibt man nun die URL

localhost:51081/API/Projects

ein und erhält dann die Ergebnisse in Textform

 

Microsoft ASP Bereitet die Daten dabei automatisch selbst auf

[{"iD_Project":1,"url":"http://microsoft.de/","title":"title1","text":"text 1"},{"iD_Project":2,"url":"https://Microsoft-Programmierer.de","title":"Title 2","text":"Text 2"}]

 

 

 

Durch das Attribut Produces("application/json) vor der API Klasse wird automatisch die Ausgabe immer aus Json als Ausgabeformat festgelegt.

[Produces("application/json")]

[Route("api/Projects")]

    public class ProjectsController : Controller

    {..

 

 

 

 

Zum Besseren Testen sollte man Erweiterungen im Browser verwenden.

Hier empfiehlt es sich oft die Chrome Browser Erweiterung Postman einzubinden

 

Das Tool findet man unter dem Chrome Shop:

"Tabbed Postman - REST Client"

ansehen: https://chrome.google.com/webstore/detail/tabbed-postman-rest-clien/coohjcphdfgbiolnekdpbcijmhambjff?utm_source=gmail

 

No separate window required! This is the legacy postman app that can run in a Chrome tab originally developed by…

No separate window required!

 

This is the legacy postman app that can run in a Chrome tab originally developed by https://www.getpostman.com/.

 

* Now runs in tab.

* Does not present itself as a Chrome App.

* Does not open a new window.

* Accessed by clicking the Postman icon in the Chrome toolbar.

 

Forked from https://github.com/postmanlabs/postman-chrome-extension-legacy

 

 

Mit Hinzufügen einbinden in den Google Chrome Browser

 

Wenn man jetzt die Test-URL eingibt wie

http://localhost:51081/api/Projects

dann kommt eine geliederte Rückgabe heraus

 

 

Hinzufügen in Client

Zum Hinzufügen trägt man dann als Type: POST ein und die URL Adresse auf die Model Class

/API/Projects

POST: http://localhost:51081/api/Projects

Zusätlich muss man in den Header-Bereich Content-Type: application/json  einfügen.

Im Ausgabe Bereich von Postman wird das Ergebnis mit Status: 201 Created ausgegeben.

 

Dabei sendet der Client folgenden Text:

POST /api/Projects HTTP/1.1
Host: localhost:51081
Content-Type: application/json
Cache-Control: no-cache

{ "url": "http://microsoft.de/", "title": "title 2 geändert", "text": "text 1" }

 

 

Create, Add unter ASP MVC

Unter ASP.Net MVC Api ist der Create Bereich zutreffend.

MVC übernimmt hier das Standard-Routing durch das Attibut [HttpPost] und die Standard-Bezeichnung Postxxxx

        // POST: api/Projects

        [HttpPost]

        public async Task<IActionResult> PostProject([FromBody] Project project)

        {

            if (!ModelState.IsValid)

            {

                return BadRequest(ModelState);

            }

 

            _context.tbl_Projects.Add(project);

            await _context.SaveChangesAsync();

 

            return CreatedAtAction("GetProject", new { id = project.ID_Project }, project);

        }

 

Ergebnis auf dem SQL Server.

Im SQL Server Object Explorer sieht man, dass die Ergebnisse angefügt wurden.

Achtung: dabei wurde scheinbar die ID falsch erhöht.

Mobile

.