For a task at work, I needed a mock WebApi for mimicking behavior of 3rd party API, that is not accessible from my machine. Sure enough I proceeded using line of least possible resistance and selected ASP.NET MVC Web Application template and WebAPI and ended up with bloated project that scared the life out of me when I just looked at it. Seriously, Microsoft, you couldn’t do a simpler template? I am pretty sure, that having all sorts MVC thingies installed helps, although, I am not completely sold on EntityFramework that sneaks in as well. But I digress.

Without further ado, I present you minimal WebAPI. A project that contains only what it needs to, to behave as WebAPI and nothing more. Below, I will lead you step by step through the process of creating one yourself, but you can also clone a repository from GitHub. Be careful though, as solution only supports .NET 4.6.x.

Step 1: Create empty web application

Open Visual studio and select ASP.NET MVC Web Application, select a name and location of your solution and click OK.  A dialog, prompting you to select additional options will show. Select Empty and uncheck every checkbox. After clicking OK, you will end up with empty web application solution. I know you might feel tempted to select WebAPI. Don’t. The end result will be nearly the same as selecting Web API from additional options.

Step 2: Add Web API to the project

First things, well, first. You could do it another way, but the easiest way is to fire up NuGet Pacakage Management Console and type the following command:

Install-Package Microsoft.AspNet.WebApi

or, if you are using older .NET version than 4.5.x:

Install-Package Microsoft.AspNet.WebApi -version 4.0.30506

This will install the following packages:

  • Microsoft.AspNet.WebApi,
  • Microsoft.AspNet.WebApi.Client,
  • Microsoft.AspNet.WebApi.Core,
  • Microsoft.AspNet.WebApi.WebHost and
  • Newtonsoft.Json.

Step 3: Add Web API configuration

Now you need to add Web API configuration. Create new folder named “App_Start”. I guess it could be named differently, but since templates put configs in “App_Start” folder, I stuck with that solution. In this folder (however you choose to name it), add a class named WebApiConfig and (for ease of use) make sure that namespace does not contain your folder name. This class should look like this:

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 }
		);
	}
}

Or, for users of .NET older than 4.5.x:

public static class WebApiConfig
{
	public static void Register(HttpConfiguration config)
	{
		config.Routes.MapHttpRoute(
		name: "DefaultApi",
		routeTemplate: "api/{controller}/{id}",
		defaults: new { id = RouteParameter.Optional }
		);
	}
}}

Now, you only need to add Global.asax file and enable Web API configuration. Code of Global.asax should look something like this:

public class Global : System.Web.HttpApplication
{

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

}

Again, for .NET older than 4.5.x, you need to do things a tad differently:

public class Global : System.Web.HttpApplication
{

	protected void Application_Start()
	{
		WebApiConfig.Register(GlobalCofiguration.Configuration);
	}

}

And… you are done. Now, you should follow de-facto standard and create a folder named Controllers for all your Api controllers. Other than that, this is it. As said above, you can clone repository or look at code on GitHub for further reference.