1 / 11

WebApi service

WebApi service. x. WebApi projekt using .NET fw 4.5/4.6. If .NET framework 4.6 You get a new menu. Choose Empty and remember to remove Azure mark. Mark WebApi. Create Web API controler (the service) This will contain the methods for the service. Start adding a controler. Right click.

jordanlopez
Download Presentation

WebApi service

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WebApi service x

  2. WebApi projekt using .NET fw 4.5/4.6 If .NET framework 4.6 You get a new menu Choose Empty and remember to remove Azure mark Mark WebApi

  3. Create Web API controler (the service)This will contain the methods for the service Start adding a controler Right click Name the controler Must end with Controler, As this is important for simple url. Chose API2 Controler - empty

  4. Web API controleren The Controlercontains the methods Basically, there will be the following types. There may be more of one type if parameters differ (or Action name set)Let methodname start with type as it provides some automation in relation to http request. • Get(HttpGet - get data – typical parameter in url / http header) • Put (HttpPut – update – typical data in http body) • Post (HttpPut – create – typical data in http body) • Delete(HttpPut – delete - typical parameter in url / http header) More Get or Put aftereachother must typicaly have the same effect, but Post og Deleteaftereachotherwill have differenteffect of cource.

  5. Implementer service methods in the controler public class StudentController : ApiController { [HttpGet] // means for routing if the method does not start with Get public Models.Student GetStudent (int id) { ………………………………………….. } [HttpGet] // means for routing if the method does not start with Get public IEnumerable<Models.Student> GetAllStudents() { ………………………………………….. } [HttpPut] // means for routing if the method does not start with Put public bool PutStudent (int id, [FromBody] Models.Student student) //Update { ………………………………………….. } [HttpPost] // means for routing if the method does not start with Post public bool PostStudent(Models.Student student) //create new { ………………………………………….. } Note there may be several methods of the same type if they have different parameters

  6. Models contains data (view) model namespace WebApiApplication01.Models { public class Student { public int Id { get; set; } public string Name { get; set; } } } Note that it is not necessary DataContract or other marker for the serialization

  7. HttpContext var httpContext = Request.Properties["MS_HttpContext"] as System.Web.HttpContextWrapper; if (httpContext.Application != null) { try { httpContext.Application.Lock(); if (httpContext.Application["TEST"] == null) ………………………………………. } finally { httpContext.Application.UnLock(); } } If you need access to Application use HTTPContext. It is not available directly in the Control Unit, but can be accessed as showed here. Remember to lock the Application of approach and change. Remember UnLock in finally to ensure it always happens. Do not store much data directly in the Application but make your own class for these. For example, create a class for a collection of objects and do locking here. Application is locked so that as little as possible. Consider using the static data instead of Application. Also remember that REST / Web API should generally be stateless In an educational context, however, we choose data in memory for not also having to involve databases every time - so we can stay focused

  8. Test of Web API Get from browser Start the web application up in the desired browser If you have not set Web.config to allow browsing you get an error But the application is now running Get methods can now be tested directly from the browser through url address Standard for server address is / api / followed by the controller same name (without Controler) followed by? And then paremetre with & in between eg here: http://localhost:1762/api/Student?id=7 Here it gives the answer as json: {"Id":7,"Name":"Student_7_name"}

  9. Test of Web API using Fiddler With Fiddler Composer you can test your Web API more advanced methods. Beside also determine whether you want json or xml. Again, you can test the PUT, POST and DELETE and include data in the HTTP body If you want json or xml specify it as additional data in the http headerContent-Type: application/json or Content-Type: application/xml By doubleclick on answeryoumight se details - eithershownformated or Raw. By Rawyoucanclicpresult and the use it as new in test Result here as json {"Id":7,"Name":"Student_7_name"}

  10. Updating (PUT) using Fiddler Here's id as a parameter in the URL address and Json data (object) in the http body

  11. Accessing of Web API in C# clientusing WebClient Using the Web Client to retrieve data from the Web API GET method System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers["Content-Type"] = "application/json"; string uriAdr = "http://localhost:1762/api/Student?id=7"; string response = webClient.DownloadString(uriAdr); Using the Web Client to update data with Web API PUT method System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers["Content-Type"] = "application/json"; string uriAdr = "http://localhost:1762/api/Student?id=7"; string uploadData = "{'Id':7, 'Name':'Student_7_name'}"; // Remark you can use ' string response = webClient.UploadString(uriAdr, ”PUT", uploadData);

More Related