How to add Web API to an existing ASP.NET MVC (5) Web Application project?

Assuming you forgot to tick the Web API checkbox (add it to the project) when making a new MVC (5) project, what do you need to do add Web API and get it working?

There are a bunch of migration questions, but none seemed to have the complete and up-to-date steps for adding Web API to an MVC 5 project and it seems to have changed from some of the old answers.

Add Web API to MVC 4

Adding GlobalConfiguration.Configure(WebApiConfig.Register) MVC 4


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Update the MVC project

Use Nuget to get the newest Web API.

Project – Right click – Manage Nuget Packages – Search for Web API (Microsoft ASP.NET Web API …) and install it to your MVC project.

Then you still need to get Web API routing to work.
From Microsoft’s Configuring ASP.NET Web API 2

Add WebApiConfig.cs to the App_Start/ folder

using System.Web.Http;

namespace WebApplication1
    public static class WebApiConfig
        public static void Register(HttpConfiguration config)
            // TODO: Add any additional configuration code.

            // Web API routes

                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }

        // WebAPI when dealing with JSON & JavaScript!
        // Setup json serialization to serialize classes to camel (std. Json format)
        var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver =
            new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

If you have an MVC Project it will have Global.asax.cs, add the new routes. Order of the Global.asax.cs routes is critical. Note there are outdated examples which use

Add this line to Global.asax.cs:

protected void Application_Start()
    // Default stuff

    // Manually installed WebAPI 2.2 after making an MVC project.
    GlobalConfiguration.Configure(WebApiConfig.Register); // NEW way
    //WebApiConfig.Register(GlobalConfiguration.Configuration); // DEPRECATED

    // Default stuff

WebAPI Help

To get the (very) helpful WebAPI help pages, install WebAPI.HelpPage.
See (~42 minutes in) for what it does. It looks very helpful!

Nuget Console: Install-Package Microsoft.AspNet.WebApi.HelpPage

To verify WebAPI is working:

To the controllers folder -> Add new item -> Web API Controller Class.

public class TestController : ApiController
    //public TestController() { }

    // GET api/<controller>
    public IEnumerable<string> Get()
        return new string[] { "value1", "value2" };

    // GET api/<controller>/5
    public string Get(int id)
        return "value";

Now you can test in IE/FF/Chrome as usual, or in the JavaScript consoles for non-get testing.

(With just the controller in the URL it will call the GET() action in the new Web API Controller, it’s automatically mapped to methods/actions depending on the REST e.g. PUT/POST/GET/DELETE. You don’t need to call them by action like in MVC)
The URL directly:


Alternatively use jQuery to query the controller.
Run the project, Open the console (F12 in IE) and try run an Ajax query. (Check your PORT & CONTROLLERNAME)
$.get( "http://localhost:PORT/api/CONTROLLERNAME/", function( data ) {
    //$( ".result" ).html( data );
    alert( "Get data received:" + data);

Side note: There are some pros/cons to consider when combining MVC and Web API in a project

WebAPI Help verification:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x