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.
The steps I needed to perform were:
Add reference to System.Web.Http.WebHost. Add App_Start\WebApiConfig.cs (see code snippet below). Import namespace System.Web.Http in Global.asax.cs. 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. 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);
}
Update 10.16.2015:
Word has it, the NuGet package Microsoft.AspNet.WebApi must be installed for the above to work.
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.
Install-Package Microsoft.AspNet.WebApi
now. See nuget.org/packages/Microsoft.AspNet.WebApi
To add WebAPI in my MVC 5 project.
Open NuGet Package manager console and run PM> Install-Package Microsoft.AspNet.WebApi
Add references to System.Web.Routing, System.Web.Net and System.Net.Http dlls if not there already Right click controllers folder > add new item > web > Add Web API controller Web.config will be modified accordingly by VS Add Application_Start method if not there already protected void Application_Start()
{
//this should be line #1 in this method
GlobalConfiguration.Configure(WebApiConfig.Register);
}
Add the following class (I added in global.asax.cs file) public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Modify web api method accordingly namespace
As soon as you add a "WebApi Controller" under controllers folder, Visual Studio takes care of dependencies automatically;
Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project 'MyTestProject'. The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API. Add the following namespace references: using System.Web.Http; using System.Web.Routing; If the code does not already define an Application_Start method, add the following method: protected void Application_Start() { } Add the following lines to the beginning of the Application_Start method: GlobalConfiguration.Configure(WebApiConfig.Register);
Install-Package Microsoft.AspNet.WebApi
2. Add new item "Web API Controller Class (v2.1)". Result: adds the api controller but does not change Application_Start
. With Owin.
You can install from nuget as the the below image:
https://i.stack.imgur.com/5JbUu.png
Or, run the below command line on Package Manager Console:
Install-Package Microsoft.AspNet.WebApi
Before you start merging MVC and Web API projects I would suggest to read about cons and pros to separate these as different projects. One very important thing (my own) is authentication systems, which is totally different.
IF you need to use authenticated requests on both MVC and Web API, you need to remember that Web API is RESTful (don't need to keep session, simple HTTP requests, etc.), but MVC is not.
To look on the differences of implementations simply create 2 different projects in Visual Studio 2013 from Templates: one for MVC and one for Web API (don't forget to turn On "Individual Authentication" during creation). You will see a lot of difference in AuthencationControllers.
So, be aware.
NOTE : this is just an abbreviation of this answer above
Open NuGet Package manager console and run PM> Install-Package Microsoft.AspNet.WebApi
Add references to System.Web.Routing, System.Web.Net and System.Net.Http dlls if not there already Add the following class public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Add Application_Start method if not there already (in global.asax.cs file) protected void Application_Start()
{
//this should be line #1 in this method
GlobalConfiguration.Configure(WebApiConfig.Register);
}
Right click controllers folder > add new item > web > Add Web API controller namespace
The above solution works perfectly. I prefer to choose Web API option while selecting the project template as shown in the picture below
Note: The solution works with Visual Studio 2013 or higher. The original question was asked in 2012 and it is 2016, therefore adding a solution Visual Studio 2013 or higher.
https://i.stack.imgur.com/3vx1R.gif
I had same problem, the solution was so easy
Right click on solotion install Microsoft.ASP.NET.WebApi from "Manage Nuget Package for Sulotion"
boom that's it ;)
Success story sharing
System.Net.Http
as well, but apart from that, it worked like a charm!