How To : .NET Framework Empty Project To Web API 2

We are going to learn how to build upon an Empty .NET Framework Project to a Web API2 Project

Created a ASP.NET Web Application (.NET Framework) Project

Create Project

ProjectCreation 2

Click "OK", we get an Empty Project

Empty Solution

Just run the Application and we get the following, since its an empty project, we will see a 403 Http Error
403 - Http Error

Lets take a look at the references folder of the project.

References image - Before

Right now we only have System.Web reference and no API related references.
First lets add that (using Nuget Package manager)

Right click on References and click on "Manage Nuget Packages…"
Go to Browse tab if not selected
Search for "web api"

PackageManager

** side note, you can install any updates you see in Nuget Package manager if you want at this time.

Check the list of package its installing and click OK.

Install Package

You may have to click "Accept" on the next screen if asked.

List of Packages installing

Nuget Package manager installs the necessary packages related to Web API. You can verify from the References folder

** Sometimes right after installing a version of a package, you may also see more updates in the package manager console. Please install them as well.

Now that we have installed the WEB API related package, we should be able to add other pieces like
1. Route config 2. Global.asax to register the route config 3. the API controller and Actions within

And then Test the API.

First lets add the RouteConfig file, which would have the Route configuration for the API, here you can set the default route settings and also add more route configurations.

To organize the code, first I have created a folder called "Startup" and then a static ".cs" file called RouteConfig.cs
RouteConfigFolderFile

Update the class contents as below.

    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApiRoute",
            routeTemplate : "api/{controller}/{id}",
            defaults : new { id = RouteParameter.Optional}
            );
    }

** you may have to add a using statement "using System.Web.Http;"

Basically what we have here is that we are registering a Route by Adding the above route to the Routes collection.

** NOTE: I have also updated the namespace as below From : namespace WebApplication2.Startup
To: namespace WebApplication2

Now that we have a route, lets add another folder for all our API Controllers called "controllers"
And add a webapi2 controller called "ValuesController.cs" as MS default api controller

Two way we can create an API Controller.

  1. Creating the default template "ValuesController"

Right click on "Controllers" folder and pick the template as shown below
ValuesControllerTemplate

In the next window you can give the controller a name, make sure your name ends with "Controller". And Click "OK"

Now we have a sample template controller called "ValuesController" with sample HTTP Actions too.

2.Creating a controller and giving it any name we prefer.

Right click on "Controller" and pick as shown below

Now we have created an API SampleController but with no actions defined which we have to.

If you see the difference between the two API controllers created, the first Template one has some template code added for us but the second one is empty.

Now let us run the App and try to hit the APIs
We still get the 403 Forbidden error on the page,
403 - Http Error

since we have to hit the API endpoints, as mentioned in the RouteConfig.cs file, we have to change our URL to match the Route defined in the config file

But now we see this
404 error image.
404 - Http Error

The problem here is that, even though we have the necessary package added and the appropriate route setup, we didn’t actually initialize the routes in the application.

To do that we have to add the RouteConfig configuration to the startup of the project.
Lets fix that by adding a "Global.asax" file and add the RouteConfig configuration in there to initialize the route.
Global asax file

Under Application_Start method, we have to Register the Route Configuration defined in RouteConfig.cs file
** We have to add some Using statements as well.

Application_Start code as below.

    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configure(RouteConfig.Register);
    }

NOTE: Add this using "using System.Web.Http;"

Now run the application and go to the API URL route and try to hit the Values Controller endpoint

API Success

** we can change the format of data the API can return and set default formats or restrict the data format etc.

DONE… That is all, only 3 steps

1. Add the WEB API Nuget Packages
2. Add the RouteConfig.cs, to define the Map Route Collection
3. Add Global.asax and Configure the Route Registration in Application_Start

Here is the GitHub Link - completed project. Not sure why you would need this, but I am just putting it out there.

Please let me know your thoughts.

Thank you
Vijaya Malla
@vijayamalla