Ad

Tuesday, January 08, 2013

MVC Web API

MVC Web API is used by some companies to provide APIs to let users access data. Most APIs are generally a REST type which have calls with names of GET (gets information), POST (sends data), PUT (update data), and Delete (removes data).

It sometimes can be difficult to figure out to create the calls if you are doing it for the first time. For instance passing complex objects for the GET and POST calls can be confusing. To pass complex objects you use an identifier for the object being passed. For example for a GET call the method would look like this:

public Product GetCrappyCivilBIMProduct([FromUri]Manufacturer autodesk)

The [FromUri] indicates MVC should look for the manufacturer object from the Uri.

For POST calls you would send the data within the request rather from the Uri. To do so you’d use something similar to this:

public Product PostCivilBIMProduct([FromBody]Manufacturer)

MVC will then look for the object within the request. If you try to use the [FromBody] for a GET it won’t work. If it you don’t set up the identifiers correctly the data will tend to not come through and show up as NULL.

I thought this post needed a picture.

SNAGHTMLb67c29e

No comments:

LinkWithin

Blog Widget by LinkWithin

Ad