If you want to use WebAPI inside an existing MVC (5) project you have to do the following steps:
1.Add WebApi packages:
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.WebHost
Newtonsoft.Json
2.Add WebApiConfig.cs
file to App_Start
folder:
using System.Web.Http;
namespace WebApiTest
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
3.Add the following line to Glabal.asax
:
GlobalConfiguration.Configure(WebApiConfig.Register);
Important note: you have to add above line exactly after AreaRegistration.RegisterAllAreas();
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//\\
GlobalConfiguration.Configure(WebApiConfig.Register);
//\\
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
– Whitehorse