Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wish to add an ASP.NET Web API to an ASP.NET MVC 4 Web Application project, developed in Visual Studio 2012. Which steps must I perform to add a functioning Web API to the project? I'm aware that I need a controller deriving from ApiController, but that's about all I know.

Let me know if I need to provide more details.

share|improve this question
add comment

3 Answers

up vote 85 down vote accepted

The steps I needed to perform were:

  1. Add reference to System.Web.Http.WebHost.
  2. Add App_Start\WebApiConfig.cs (see code snippet below).
  3. Import namespace System.Web.Http in Global.asax.cs.
  4. Call WebApiConfig.Register(GlobalConfiguration.Configuration) in MvcApplication.Application_Start() (in file Global.asax.cs), before registering the default Web Application route as that would otherwise take precedence.
  5. Add a controller deriving from System.Web.Http.ApiController.

I could then learn enough from the tutorial (Your First ASP.NET Web API) to define my API controller.

App_Start\WebApiConfig.cs:

using System.Web.Http;

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });
    }
}

Global.asax.cs:

using System.Web.Http;

...

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
share|improve this answer
4  
This was really helpful. I had to add a reference to System.Net.Http as well, but apart from that, it worked like a charm! –  Christofer Eliasson Aug 24 '12 at 13:06
4  
I upgraded a project from MVC3 to 4 as well and also needed to add a reference to System.Web.Http. –  Damien Sawyer Oct 16 '12 at 22:16
1  
I created an "Internet Application" and it seems that all of this is done for you out of the box. –  makerofthings7 Jan 29 at 3:08
1  
@longda The Web API route in this answer means that requests to /api/* get routed to API controllers. I don't think it matters if you put your API controllers in a sub-directory ("api"), although I've done this in my projects. Just try for yourself. –  aknuds1 Feb 8 at 23:21
1  
@longda Although it isn't required to be in an Area "api" it plays nicer with the Html helpers - well, kinda. The helpers themselves are in need of a little help when it comes to areas imho. –  Dave Jellison Nov 15 at 18:02
show 6 more comments

When creating ASP.NET MVC 4 Web Application project, even empty project, the package Microsoft ASP.NET Web Api is added by default.

So for the first baby step, just create one test api controller which inherits from ApiController from namespace System.Web.Http and then learn tutorial to follow.

share|improve this answer
 
So, what else do I need to do except add the controller? This is why I posted this question in the first place, the tutorial doesn't really say since it assumes a Web API project. I've added an API controller, but it isn't routed to. –  aknuds1 Aug 17 '12 at 6:57
 
The tutorial wasn't of much help as regards adding a Web API to an existing project, so I figured it out from a Web API project, as outlined in my answer. –  aknuds1 Aug 17 '12 at 7:34
 
I agree, it seems that this plumbing is already installed if you use the Web App project template. –  longda Feb 8 at 22:56
add comment

UPDATE 11/22/2013 - this is the latest WebApi package:

Install-Package Microsoft.AspNet.WebApi

Original answer (this is an older WebApi package)

Install-Package AspNetWebApi

More details: http://wildermuth.com/2012/2/22/WebAPI_for_the_MVC_Guy

share|improve this answer
2  
As of 2013 that is a legacy package and you want Install-Package Microsoft.AspNet.WebApi now. See nuget.org/packages/Microsoft.AspNet.WebApi –  Chris Aug 13 at 2:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.