Likes

Developing Windows Azure and Web Services Lab @ 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 ActionFilter 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 ActionFilterAttribute.
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: