.NET WEB API: REST Endpoints with C#

2 Mar

I have posted here and here on using MVC 5 to edit a database with no code but as much as I like MVC 5, there are times that I just want a service and I will code up a front-end from scratch. This also allows anyone else in my organization to use the service as they wish as well. In this post, I will walk you through using the Web API to connect a database table as a REST Endpoint and allow you to view records, add a record, modify a record and delete a record.

The Steps

First, create a new ASP .NET Application

1

 

Choose WEB API

2

Right click on Models and ADD ADO.NET Entity.

3

Choose the first option.

4

Enter your database and connection properties. You can then pick the database you want to use.

5

Select the table or tables you would like to make available.

6

Build the project. If you do not build at this point, you will get errors in the next steps.

7

Right click on Controller and add a new scaffolding item.

8

Pick the Web API 2 controller using Entity Framework.

9

Select the Model (Name chosen when you brought in the table). Also pick the content class you used in the first steps. It is a drop down and if you pick the wrong one you will get an error. Just retry choosing a different one if you can’t remember what you named it.

10

Launch the application in Chrome and go to the URL for your controller. The default is http://localhost:xxxx/api/controllername. Notice I am getting XML returned. This is not what I want.

11

Under the application start folder, open the WebApiConfig.cs file. Add these two lines:

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == “application/xml”);
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

 

12

Now browse to the URL again. JSON!

13

You can also add the primary key at the end of the url to get back a single record.

14

I will use the DHC Chrome plugin to show you how to make calls to the other endpoint not using GET. This is a fast way to test without having to write our front-end. POST to the URL and add Mr. T.

{“id”:5,”name”:”Mr. T”}

15In our application, we can see Mr T was added.

16

Let’s use PUT and edit Mr. T using his other name: B.A. Baracus.

17

It has been edited in our application.

18

Now let’s DELETE Mr. T.

19

He has been removed.

20

No code and we can view, add, edit and delete records in our database. Write a front-end using AJAX calls to each of the endpoints and pass a string representation of the object and you can do the same thing.

A note on posting to the endpoint.

Create a JavaScript object like Mr.T.

var mrT={“id”:5,”name”:”Mr. T”};

Now, to send it, you need to make it a string.

http.send(JSON.stringify(mrT));

This will allow your application to work properly. I made the mistake of trying to use parameters in the URL and that was a no go.

 

Advertisements

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: