Adding Web API Controller To Existing MVC Project

If you have MVC project and you need to add Web API controller to this project, it can be done very easy.

1. Add Nuget package Microsoft.AspNet.WebApi.Core. This will add references to System.Web.Http.dll.

2. Add registration file for Web API. It would better to do it like Visual Studio does – add file App_Start\WebApiConfig.cs

3. Add using System.Web.Http; to Global.asax.cs

4. Add GlobalConfiguration.Configure(WebApiConfig.Register); in Application_Start() method before registering MVC routes.

Application_Start() would be like this.

5. Create a controller and derive it from System.Web.Http.ApiController

Web API Anonymous Authentication And IIS Express

I’ve been tested a Web API web service and I needed an anonymous authentication. This can be done with attribute [AllowAnonymous].
You can decorate a whole controller

or a particular method(s)

The most curious thing is that it does not work when you run Visual Studio debugger! The secret is hidden in the properties of the Web API project. Click the project in Solution Explorer, then press F4 (don’t choose Properties from context menu). Here you will see authentication settings.


All these properties are saved in .csproj file.