Web Api service x Web Api projekt using
Web. Api service x
Web. Api 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 Web. Api
Create Web API controler (the service) This will contain the methods for the service Start adding a controler Name the controler Must end with Controler, As this is important for simple url. Right click Chose API 2 Controler - empty
Web API controleren The Controler contains 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 (Http. Get - get data – typical parameter in url / http header) • Put (Http. Put – update – typical data in http body) • Post (Http. Put – create – typical data in http body) • Delete (Http. Put – delete - typical parameter in url / http header) More Get or Put after each other must typicaly have the same effect, but Post og Delete after each other will have different effect of cource.
Implementer service methods in the controler public class Student. Controller : Api. Controller { [Http. Get] // means for routing if the method does not start with Get public Models. Student Get. Student (int id) { ……………………. . } [Http. Get] // means for routing if the method does not start with Get public IEnumerable<Models. Student> Get. All. Students() { ……………………. . } [Http. Put] // means for routing if the method does not start with Put public bool Put. Student (int id, [From. Body] Models. Student student) //Update { ……………………. . } [Http. Post] // means for routing if the method does not start with Post public bool Post. Student(Models. Student student) //create new { ……………………. . } Note there may be several methods of the same type if they have different parameters
Models contains data (view) model namespace Web. Api. Application 01. Models { public class Student { public int Id { get; set; } public string Name { get; set; } } } Note that it is not necessary Data. Contract or other marker for the serialization
Http. Context var http. Context = Request. Properties["MS_Http. Context"] as System. Web. Http. Context. Wrapper; if (http. Context. Application != null) { try { http. Context. Application. Lock(); if (http. Context. Application["TEST"] == null) ……………………. } finally { http. Context. Application. Un. Lock(); } } 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 Un. Lock 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
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"}
Test of Web API using Fiddler If you want json or xml specify it as additional data in the http header Content-Type: application/json or Content-Type: application/xml By doubleclick on answer you might se details - either shown formated or Raw. By Raw you can clicp result and the use it as new in test Result here as json {"Id": 7, "Name": "Student_7_name"} 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
Updating (PUT) using Fiddler Here's id as a parameter in the URL address and Json data (object) in the http body
Accessing of Web API in C# client using Web. Client Using the Web Client to retrieve data from the Web API GET method System. Net. Web. Client web. Client = new System. Net. Web. Client(); web. Client. Headers["Content-Type"] = "application/json"; string uri. Adr = "http: //localhost: 1762/api/Student? id=7"; string response = web. Client. Download. String(uri. Adr); Using the Web Client to update data with Web API PUT method System. Net. Web. Client web. Client = new System. Net. Web. Client(); web. Client. Headers["Content-Type"] = "application/json"; string uri. Adr = "http: //localhost: 1762/api/Student? id=7"; string upload. Data = "{'Id': 7, 'Name': 'Student_7_name'}"; // Remark you can use ' string response = web. Client. Upload. String(uri. Adr, ”PUT", upload. Data);
- Slides: 11