DPS907 notes – Mon Sep 14

Our first hands-on session in a computer-lab.

.

Tentative agenda

9:50am
Class timeslot begins
Turn on (or login to) a computer
Start Visual Studio 2013; look for a desktop folder named “Visual Studio”

10:00am
Professor will guide you as you learn how to create, code, and test a simple web service

10:30am (approximately)
Controller method return type discussion

10:40am
Introduce Lab 1, and begin working on it

11:10am
Your work will be partially graded before you leave the room
(it will take us about 20 minutes to visit each student and grade your work)

11:35am
Our class timeslot ends

.

Controller method return type

As you begin this Web Services course, you must learn about method return types.

You have already learned that the ASP.NET Web API request-handling pipeline serializes to and from JSON and XML. If your method return type is an object, or a collection of objects, the serializer does its job, and the user receives valid data in the response.

However, we want to improve on this. We will use a return type IHttpActionResult for all methods except “Delete”, which uses void as its return type. The improvement is that the request-handling pipeline adds the correct HTTP status code to the response.

When an ApiController method returns an IHttpActionResult, we use a specific helper method, depending upon the method logic. Most of the helper methods accept one or more arguments, which is how you actually return an object or collection of objects.

.

Here is an alphabetically-ordered list of helper methods:

BadRequest (returns an HTTP 400 status code)

Conflict (409)

Created (201)

InternalServerError (500)

Json (200)

NotFound (404)

Ok (200)

Redirect (302)

Unauthorized (401)

.

Here is the same list, ordered by HTTP status code:

Ok (200)

Json (200)

Created (201)

(remember – a void method return type will deliver HTTP 204)

Redirect (302)

BadRequest (400)

Unauthorized (401)

NotFound (404)

Conflict (409)

InternalServerError (500)

.

If you’re interested, the origin of these methods trace back to the System.Web.Http.Results namespace.

.

Fiddler testing patterns

As you begin this Web Services course, use the following testing patterns when using the Fiddler tool:

.

Get all request

  • Method is GET
  • URI is the collection URI
  • Optionally, you can specify an Accept header

.

Get one request

  • Method is GET
  • URI includes a segment that has an identifier
  • Optionally, you can specify an Accept header

.

Add new request

  • Method is POST
  • URI is the collection URI
  • Must add a Content-Type header
  • Optionally, you can specify an Accept header if you want the return type to be different from the posted Content-Type
  • Include a correctly-defined-and-formatted entity body

 

The following is an example of a correctly-formatted “add new” request. Click to open full-size in its own tab/window.

fiddler-how-to-post

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
%d bloggers like this: