2

I had an ASP.NET MVC project* that was created when VS 2013 first came out. (Aside: how do I find out the MVC version used for an existing project?). I have it now opened in VS 2015 and added a Web API 2 controller.

A static WebApiConfig class was added by VS. However, the Register method wasn't called. As a result, my api route is not recognized.

How/where can I call the WebApiConfig.Register method and what is the parameter that has to be passed to this method?


* This is the version where there is an App_Start folder with BundleConfig.cs, FilterConfig.cs, IdentityConfig.cs, RouteConfig.cs, StartupAuth.cs, WebApiConfig.cs.

Old Geezer
  • 14,854
  • 31
  • 111
  • 198

2 Answers2

6

As stated in this post, you should use GlobalConfiguration.Configure and not WebApiConfig.Register for any new (or migrated) Web API 2 project:

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    //MVC Registrations
}

Also be sure to update any NuGet Package in your project to the last version.

Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
  • Thanks. This solved the problem. Both the normal MVC and api routes work nicely. – Old Geezer May 31 '16 at 17:03
  • Applies to Web API 1. Web API 2 does not work this way. If you created project with Visual Studio 2017 or your Web API project uses .NET Core 2, then you probably use Web API 2 and this is not the solution. – Sergei G Mar 07 '18 at 23:25
1

In your Global.ascx.cs

using System.Web.Http;

protected void Application_Start()
{
    //other stuff            
    //update: do not use WebApiConfig.Register(GlobalConfiguration.Configuration)
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

how do I find out the MVC version used for an existing project?

What MVC does it reference? ie what is the version number of theSystem.Web.Mvc assembly which your web project will reference

wal
  • 17,409
  • 8
  • 74
  • 109
  • I have just added more information in my question regarding the project type. In my `Application_Start`, `GlobalConfiguration` is not in scope. Where can I find this class or object? – Old Geezer May 31 '16 at 10:51
  • add `using System.Web.Http;` – wal May 31 '16 at 10:54
  • Thanks. If I put `WebApiConfig.Register` before `RouteConfig.RegisterRoutes`, the browser in debugger mode will just close by itself. If I reverse the sequence, things seem to be normal, but /api/xx still give a 404. What could still be wrong? *BTW, my System.Web.Mvc has a `Runtime Version` of v4.0.30319 and a `Version` of 5.2.0.0 as seen in Properties.) – Old Geezer May 31 '16 at 11:26
  • yes so you are using mvc5. (runtime version is the version of the *framework* itself, ie .NET4 CLR in this case) - re: your 404, pls see my answer on mixing web api and mvc http://stackoverflow.com/questions/34371613/exposing-a-few-calls-from-an-existing-asp-net-mvc-site-to-other-rest-clients-wit/34584670#34584670 as per that link `WebApiConfig.Register` shld be before `RouteConfig.RegisterRoutes` - i'm not sure what you mean by 'close by itself' <-- you should break on Exceptions or figure out why its doing that - – wal May 31 '16 at 11:31
  • The browser closed because the server had a 500 error had pushed out `Content-Type:application/xml;` An error has occurred. – Old Geezer May 31 '16 at 15:48