Implement the travelers' service by using ASP.NET Web API. Start by creating a new ASP.NET Web API controller, and implement CRUD functionality using the POST, GET, PUT and DELETE HTTP methods.
The main tasks for this exercise is to create a new API controller for the Destinations service and implement the controller actions.
To implement the required functionality, you need to perform the following steps:
1. Browse to the location where the Exercise 01 .zip file is saved.
2. Extract the files.
3. Press the Window logo key. The Start screen is displayed.
4. Start typing Visual Studio 2012.
5. Right-click the Visual Studio 2012 tile. The App bar is displayed.
6. Click the Run as administrator button on the App bar.
Note: If User Account Control dialog box is displayed, click the Yes button. If the current user account does not have administrative rights, the User Account Control dialog box prompts for the administrator credentials. In this case, specify the administrator credentials, and then click the Yes button.
7. Select FILE—Open—Project/Solution. The Open Project dialog box is displayed.
8. Browse to the location where the Exercise 01 .zip file is extracted.
9. Double-click the Exercise 01 folder.
10. Double-click the BlueYonder.Companion folder.
11. Select the BlueYonder.Companion.sln file.
12. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio window is displayed.
13. Ensure that the Solution Explorer window is opened.
14. Right-click the BlueYonder.Companion.Controllers node, and then select Add—New Item. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
15. Select Web under the Visual C# Items node in the left pane.
16. Ensure that Web API Controller Class is selected in the middle pane.
17. Select and replace the existing text in the Name textbox with TravelersController.
18. Click the Add button. The TravelersController.es file is displayed.
19. Type the highlighted portions of the following code snippet in the TravelersController.es file: using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BlueYonder.Entities;
using BlueYonder.DataAccess.Interfaces;
using BlueYonder.DataAccess.Repositories;
20. Replace the existing code within the TravelersController class in the TravellerController.es file with the following code snippet:
private ITravelerRepository Travelers { get; set; } public TravelersController()
{
Travelers = new TravelerRepository();
}
public HttpResponseMessage Get(string id)
{
var traveler = Travelers.FindBy(t => t.TravelerUserldentity = id).FirstOrDefault();
//Handling the HTTP status codes if (traveler != null)
return Request.CreateResponse<Traveler>(HttpStatusCode.OK, t
else
return Request.CreateResponse(HttpStatusCode.NotFound);
}
public HttpResponseMessage Post(Traveler traveler)
{
//Saving the new order to the database Travelers.Add(traveler);
Travelers.Save();
// creating the response, the newly saved entity and 201 Created var response = Request.CreateResponse(HttpStatusCode.Created, tr response.Headers.Location = new Uri(Request.RequestUri, traveler.TravelerId.ToString());
return response;
}
public HttpResponseMessage Put(String id, Traveler traveler)
{
// returning 404 if the entity doesn’t exist
if (Travelers.FindBy(t => t .TravelerUserldentity == id). FirstOrDe-f
null)
return Request.CreateResponse(HttpStatusCode.NotFound);
Travelers.Edit(traveler);
Travelers.Save();
return Request.CreateResponse(HttpStatusCode.OK);
}
public HttpResponseMessage Delete(St ring id)
{
var traveler = Travelers.FindBy(t => t.TravelerUserldentity id). FirstOrDefaultQ;
// returning 404 if the entity doesn’t exist if (traveler == null)
return Request.CreateResponse(HttpStatusCode.NotFound); Travelers.Delete(traveler);
Travelers.Save();
return Request.CreateResponse(HttpStatusCode.OK);
}
21. Ensure that the Solution Explorer window is opened.
22. Ensure that the BlueYonder.Companion.Host node is expanded.
23. Double-click the Web.config file under the BlueYonder.Companion.Host node. The Web.config file is displayed.
24. Locate the text User ld=BlueYonder;Password=Pa$$wOrd; in the connection string, and replace it with User ld=sa;Password=password@123; in the Web.config file.
25. Select FILE—Save All to save the changes.
26. Close Microsoft Visual Studio 2012.
No comments:
Post a Comment