Developing Windows Azure and
Web Services Lab @ home 6 Ans 1
OData
is a data access protocol that provides standard CRUD access of a data source
via a website. To add support for OData protocol you will install a NuGet
Microsoft.AspNet.WebApi.OData package, and then decorate the methods that you
want to support OData with [Queryable] attribute.
The main tasks for this exercise are:
1. Add a queryable action to the flight schedule service.
2. Handle the search event in the client application and query the flight schedule service by using OData filters.
The main tasks for this exercise are:
1. Add a queryable action to the flight schedule service.
2. Handle the search event in the client application and query the flight schedule service by using OData filters.
Task 1: Adding a queryable action to the flight schedule service
To add a queryable action to the flight schedule service, you need to perform the following steps:
1. Browse to the location where the Exercise 03.zip file is saved.
2. Extract the files.
3. Press the Windows logo key. The Start screen is displayed.
4. Start typing Visual Studio 2012. The Search pane and Apps screen are displayed.
5. Right-click the Visual Studio 2012 tile, and then select Run as Administrator.
6. Select FILE—Open—Project-Solution. The Open Project dialog box is displayed.
7. Browse to the location where the Exercise 03 folder is extracted.
8. Double-click the Exercise 03 folder.
9. Double-click the BlueYonder.Companion folder.
10. Select the BlueYonder.Companion.sin file.
11. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio window is displayed.
12. Ensure that the Solution Explorer window is opened.
13. Click the TOOL S menu on the toolbar, and then select Library Package Manager—Package Manager Console. The Package Manager Console window is displayed.
14. Type install-package Microsoft.AspNet.WebApi.OData -version 0.1.0-alpha-120815 -ProjectName BlueYonder.Companion.Controllers, and then press the Enter key.
15. Wait until Package Manager Console finished downloading and adding the package.
16. Ensure that the Solution Explorer window is opened.
17. Expand the BlueYonder.Companion.Controllers node.
18. Double-click the LocationsController.es file. The LocationsController.es file is displayed.
19. Replace the existing Get? {) method, which has the three parameters with the following code snippet in the LocationsController.es file:
[Queryable]
public IQueryable<Location> Get()
{
return Locations.GetAll();
}
20. Select FILE—Save All to save the changes.
21. Close Microsoft Visual Studio 2012.
Task 2: Handling the Search Event in the Client Application and Query the Flight Schedule Service by Using OData filters
To handle the search event in the client application and query the flight schedule service by Using OData filters, you need to perform the following steps:
1. Press the Windows logo key. The Start screen is displayed.
2. Start hyping Visual Studio 2012. The Search pane and Apps screen are displayed.
3. Right-click the Visual Studio 2012 tile, and then select Run as Administrator.
4. Select FILE—Open—Projecb'Solution. The Open Project dialog box is displayed.
5. Browse to the location where the Exercise 03 folder is extracted.
6. Double-click the Exercise 03 folder.
7. Double-click the BlueYonder.Companion.Client folder.
8. Select the BlueYonder.Companion.Client.sln file.
9. Click the Open button. The BlueYonder.Companion.Client - Microsoft Visual Studio window is displayed.
10. Ensure that Solution Explorer window is opened.
11. Expand the BlueYonder.Comapanion.Shared node.
12. Double-click the Addresses.es file. The Addresses.es file is displayed.
13. Replace the return statement of the GetLocationsWithQueryUri property with the following code snippet:
return GetLocationsUri + M?Sfilter=substringof(tolower(,{0},),tolower(City))";
14. Select FILE— Save All to save the changes.
15. Close Microsoft Visual Studio 2012.
Developing Windows Azure and
Web Services Lab @ home 6 Ans 2
To make your web
application more robust, you need to add some validations to the incoming data.
You will apply validation rule to your server to verify that all required
fields of a model are sent from the client before handling the request. You
will decorate the Travel model with attributes that define the required fields,
then you will derive from the Action Filter class and implement the validation
of the model. Finally, you will add the validation to the Post action.
The main tasks for this exercise are:
1. Add data annotations to the Traveler class.
2. Create a class that derives from Action Filter Attribute.
3. Implement the action filter to validate the model.
4. Add a custom attribute to the put and post actions in the booking service.
The main tasks for this exercise are:
1. Add data annotations to the Traveler class.
2. Create a class that derives from Action Filter Attribute.
3. Implement the action filter to validate the model.
4. Add a custom attribute to the put and post actions in the booking service.
To implement the required functionality, you need to perform the following steps:
1. Browse to the location where the Exercise 04.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 theVisual Studio 2012 tile. The App bar is displayed.
6. Click the Run as administrator button.
7. Select FILE—Open—Project/Solution. The Open Project dialog box is displayed.
8. Browse to the location where the Exercise 04.zip file is extracted.
9. Double-click the Exercise 04 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 (Administrator) window is displayed.
13. Ensure that the Solution Explorer window is opened.
14. Expand the BlueYonder.Entities node.
15. Double-click the Traveler.es file. The Traveler.es file is displayed.
16. Type the highlighted portions of the following code snippet in the Traveler.es file:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace BlueYonder.Entities
{
public class Traveler
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Travelerld { get; set; }
public string TravelerUserldentity { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Phone]
public string MobilePhone { get; set; }
[Required]
public string HomeAddress { get; set; }
public string Passport { get; set; }
[EmailAddress]
public string Email { get; set; }
>
}
17. Select FILE—Save All to save the changes.
18. Right-click the BlueYonder.Companion.Controllers project in the Solution Explorer window, and then select Add—New Folder.
19. Type ActionFilters. and then press the Enter key.
20. Right-click the ActionFilters folder, and then select Add—Class. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
21. Select and replace the existing text in the Name text box with ModelValidationAttribute.
22. Click the Add button. The ModelValidationAttribute.es file is displayed.
23. Type the highlighted portions of the following code snippet in the ModelValidationAttribute.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Filters;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace BlueYonder.Companion.Controllers.ActionFilters
{
public class ModelValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(
System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModclStatc.IsValid)
actionContext.Response = actionContext.Request.CreateErrorRespons HttpStatusCode.BadRequest, new HttpError(actionContext.ModelState, true));
>
}
}
24. Select FILE— Save All to save the changes.
25. Ensure that the BlueYonder.Companion.Controllers node is expanded in the Solution Explorer window.
26. Double-click the TravelersController.es file. The TravelersController.es file is displayed.
27. Type the highlighted portion of the following code snippet in the TravelersController.es file:
using BlueYonder.Companion.Entities; using BlueYonder.DataAccess.Repositories; using BlueYonde r.Companion.Controlle rs.ActionF ilters; namespace BlueYonder.Companion.Controllers
28. Type the highlighted portion of the following code snippet in the TravelersController.es file: public HttpResponseMessage Get(string id)
{
var traveler = Travelers.FindBy( t -> t .Travelerllserldentity -- id) . FirstOrDefaultQ;
// Handling the HTTP status codes
if (traveler != null)
return Request.CreateResponse<Traveler>
(HttpStatusCode.OK, traveler); else
return Request.CreateResponse(HttpStatusCode.NotFound);
}
[ModelValidation]
public HttpResponseMessage Post(Traveler traveler)
{
// saving the new order to the database Travelers.Add(traveler);
Travelers.Save();
// creating the response, with three key features:
// 1. the newly saved entity // 2. 201 Created status code
// 3. Location header with the location of the new resource var response ■ Request.CreateResponse(HttpStatusCode.Created, traveler); response.Headers.Location = new Uri(Request.RequestUri, traveler.Traveler ToStringQ);
return response;
}
[ModelValidation]
public HttpResponseMessage Put(int id, [FromBody]Traveler traveler)
{
// returning 404 if the entity doesn't exist if (Travelers.GetSingle(id) == null)
return Request.CreateResponse(HttpStatusCode.NotFound);
T ravelers.Edit(traveler);
T ravelers.Save();
return Request.CreateResponse(HttpStatusCode.OK);
}
29. Select FILE—Save All to save the changes.
30. Select BUILD—Build Solution. The Build succeeded message is displayed on the status bar at the bottom of the BlueYonder.Companion - Microsoft Visual Studio (Administrator) window.
31. Close Microsoft Visual Studio 2012.
No comments:
Post a Comment