Tworzenie usług sieciowych REST API w technologii ASP.NET WebAPI - 3 pytania i odpowiedzi ze szkolenia (wrzesień 2019)

Seria 3 pytań uczestników, które pojawiły się podczas szkolenia Tworzenie usług sieciowych REST API w technologii ASP.NET WebAPI realizowanego w dniach 23-25.09.2019 r.


W jaki sposób za pomocą aplikacji ngrok wystawić stronę hostowaną na IIS Express?

W takim przypadku należy dodać parametr host-header: ngrok http -host-header=""localhost:[port]"" [port]"

Czy URL w postaci GET /api/customers/delete?customerId=3 jest prawidłowy?

Zgodnie z zasadami REST API nie jest prawidłowy. Powinien wyglądać następująco: DELETE /api/customers/3 Inne przykłady: nieprawidłowe: GET /api/customers/GetCustomers GET /api/customers?CustomerId=10 GET /api/customers?Name=vavatech GET /api/orders?CustomerName=vavatech&Year=2018 GET /api/customers/create?name=Vavatech&Street=Olesinska GET /api/customers/GetByCity?City=Lublin&Street=Dworcowa prawidłowe: GET /api/customers GET /api/customers/10 GET /api/customers/vavatech GET /api/customers/vavatech/orders GET /api/customers/vavatech/orders/2018 GET /api/customers?City=Lublin&Street=Dworcowa GET /api/orders POST /api/customers PUT /api/customers/Vavatech DELETE /api/customers/100

W jaki sposób tworzyć własne odpowiedzi w stylu Ok(), BadRequest() ?

Należy zaimplementować interfejs IHttpActionResult Na przyykład public class AuthenticationFailureResult : IHttpActionResult { public Task ExecuteAsync(CancellationToken cancellationToken) { HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); return Task.FromResult(response); } }