.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



Choose WEB API


Right click on Models and ADD ADO.NET Entity.


Choose the first option.


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


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


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


Right click on Controller and add a new scaffolding item.


Pick the Web API 2 controller using Entity Framework.


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.


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.


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”);



Now browse to the URL again. JSON!


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


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.


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


It has been edited in our application.


Now let’s DELETE Mr. T.


He has been removed.


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.


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.



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: