62

There's a great question here: How to add Web API to an existing ASP.NET MVC 4 Web Application project?

Unfortunately, it wasn't enough to solve my problem. I've tried twice to be sure I haven't done anything wrong. I right clicked on "Controllers" and added the item "Web API 2 Controller with actions, using Entity Framework" where I selected my model class and db context. Everything went fine... but still... everytime I've tried to access /api/Rest I was getting a 404 error (The name of my Controller is RestController).

Community
  • 1
  • 1
Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
  • 1
    Refer this link `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` – Jegadeesh Mar 14 '14 at 10:33
  • @Jegadeesh, thks, I'll check it – Luis Gouveia Mar 14 '14 at 10:34
  • @Jegadeesh, the provided link explained me that I should change WebApiConfig.Register(GlobalConfiguration.Configuration); to GlobalConfiguration.Configure(WebApiConfig.Register); but unfortunatelly that wasn't enough... thks though – Luis Gouveia Mar 14 '14 at 10:44
  • 1
    Can you paste what's in your App_Start/WebApiConfig.cs? Your routing stuff should be here. – Stachu Mar 14 '14 at 12:33
  • @statue, sure I can (thanks!): namespace MVC4GMAPS { 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 } ); } } } – Luis Gouveia Mar 14 '14 at 15:43
  • For a complete answer on how to add WebAPI to an existing project see: http://stackoverflow.com/questions/26067296/how-to-add-web-api-to-an-existing-asp-net-mvc-5-web-application-project/ – lko Sep 30 '14 at 19:48
  • 1
    @Jegadeesh Link was broken. Found similar content at: http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2 – Thomas Langston Jan 20 '15 at 14:57

3 Answers3

121

It's working!!! I didn't want to believe, but guess what, the problem was related with the Global.asax routing order.

While it doesn't work with:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    GlobalConfiguration.Configure(WebApiConfig.Register); //I AM THE 4th
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}      

It works with:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register); //I AM THE 2nd
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}      

Crazy, I know.

Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
  • 13
    api routes must evaluate first, or you need to create more strict mvc routes (such that the framework doesn't attempt to look for 'ApiController'.) This is not MVC5-specific behavior. – Shaun Wilson Mar 17 '14 at 13:21
  • 1
    In my case, I created Area and it's called Api. Bad idea. Keep giving me 404 regardless of my setting. So, DO NOT create Area called Api. – stack247 Oct 25 '15 at 05:16
5

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);
}
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
3

"When adding new routes ALWAYS KEEP IN MIND that you have to add specific route on the top followed by more generic route in the end. Otherwise, your web app will never receive proper routing."

The above is the citation from here: http://www.codeproject.com/Tips/771809/Understanding-the-Routing-Framework-in-ASP-NET-MVC

I know the answer is already given, but this could help to understand why we need to put GlobalConfiguration.Configure(WebApiConfig.Register); before RouteConfig.RegisterRoutes(RouteTable.Routes);

Sergey
  • 1,020
  • 11
  • 22