Home > 2011 Fall DPS907, ASP.NET, Windows > WCF Web API service, with EF data model, supporting POST, PUT, DELETE (on an ASP.NET web site)

WCF Web API service, with EF data model, supporting POST, PUT, DELETE (on an ASP.NET web site)

This post builds upon the topics in WCF Web API service with an Entity Framework data model. We add the ability to support POST, PUT, and DELETE operations.

.

This post was most recently updated in October 2011.

It is the third post in a multipart series on WCF Web API, and is intended to be used by students in Seneca’s diploma- and degree-level software development programs, and any other entry- to mid-level developer.

.

This post was inspired by…

The WCF Web API programming team discuss POST, PUT, and DELETE support in this note:

Adding support for… POST, PUT, and DELETE methods

We complement the coverage, by building on the first to posts in this series:

Getting started with a WCF Web API service

WCF Web API service with an Entity Framework data model

.

Make sure that you use your HTTP Test Client to prove that your service works correctly.

.

POST methods

The most important idea is that you must POST to a collection URI. Other features of a POST method:

  • A WebInvoke attribute is used; the URI template should be the same as the one used to GET the collection, but its method must be “POST”
  • It includes an argument for the new object to be added to the collection
  • Its return type should be HttpResponseMessage<T>, so that we can set the status code
  • The code adds the object to the context, and then saves changes

.

A sample method follows.

[WebInvoke(UriTemplate = "regions", Method = "POST")]
public HttpResponseMessage<nwRegion> AddRegion(nwRegion region)
{
    // FYI - you must supply a RegionID, because its key is NOT auto-incrementing

    // Add the region that was passed in as the argument
    context.nwRegions.Add(region);
    context.SaveChanges();

    // Prepare the response - we want to return "201 Created"
    HttpResponseMessage<nwRegion> response = new HttpResponseMessage<nwRegion>(region, HttpStatusCode.Created);
    return response;
}

.

PUT methods

The most important idea is that you must PUT to a URI for a specific object. Other features of a PUT method:

  • A WebInvoke attribute is used; the URI template should be the same as the one used to GET a specific object, but its method must be “PUT”
  • It includes an argument for the updated object, and the object key/ID
  • Its return type should be the object’s type
  • The code fetches the object to the context, and then saves changes/udpates

.

A sample method follows.

[WebInvoke(UriTemplate = "region/{id}", Method = "PUT")]
public nwRegion RegionUpdate(nwRegion region, string id)
{
    // Local variables
    int _id;
    nwRegion _object;

    // If the argument is not an integer, return an error
    if (!Int32.TryParse(id, out _id)) throw new HttpResponseException(HttpStatusCode.NotFound);

    // Attempt to locate the object
    _object = context.nwRegions.Find(_id);

    // If the object is null, return an error
    if (_object == null) throw new HttpResponseException(HttpStatusCode.NotFound);

    // Update the changed values
    context.Entry(_object).CurrentValues.SetValues(region);
    context.SaveChanges();

    return _object;
}

.

DELETE methods

The most important idea is that you must DELETE to a URI for a specific object. Other features of a DELETE method:

  • A WebInvoke attribute is used; the URI template should be the same as the one used to GET a specific object, but its method must be “DELETE”
  • It includes an argument for the object key/ID
  • Its return type should be the object’s type (or HttpResponseMessage, with an empty Content property, and an HttpStatusCode of OK)
  • The code fetches the object to the context, and then deletes it

.

A sample method follows.

[WebInvoke(UriTemplate = "region/{id}", Method = "DELETE")]
public nwRegion RegionDelete(string id)
{
    // Local variables
    int _id;
    nwRegion _object;

    // If the argument is not an integer, return an error
    if (!Int32.TryParse(id, out _id)) throw new HttpResponseException(HttpStatusCode.NotFound);

    // Attempt to locate the object
    _object = context.nwRegions.Find(_id);

    // If the object is null, return an error
    if (_object == null) throw new HttpResponseException(HttpStatusCode.NotFound);

    // Delete the object
    context.nwRegions.Remove(_object);
    context.SaveChanges();

    return _object;
}

.

On your own

In your web service, add methods that enable you to support POST, PUT, and DELETE. You will also get practice by doing Lab 2.

.


.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: