Just a simple example, showing how to self-host ASP.NET Web API.
- On an elevated console (“Run as administrator”), execute “netsh http add urlacl url=http://+:8080/ user=<your user name>”, to allow the running user to listen on port 8080.
- On Visual Studio, create a “Console Application” project.
- Change the project’s Target Framework property from “.NET Framework 4 Client Profile” to “.NET Framework 4 profile”.
- Install the AspNetWebApi.Selfhost NuGet package
- Create a public ApiController derived class.
public class HelloController : ApiController { public HttpResponseMessage Get() { return new HttpResponseMessage { Content = new StringContent("Hello HTTP") }; } }
- In the program’s Main method,
- Create a HttpSelfHostConfiguration, initialized with the base address.
- Add the default route: MapHttpRoute(“default”, “{controller}/{id}”, new { id = RouteParameter.Optional });
- Create a HttpSelfHostServer, initialized with the above configuration object.
- Open the server, bearing in mind that the OpenAsync method is asynchronous.
static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:8080"); config.Routes.MapHttpRoute("default", "{controller}/{id}", new { id = RouteParameter.Optional }); var server = new HttpSelfHostServer(config); server.OpenAsync().Wait(); Console.WriteLine("Server is opened"); Console.ReadKey();
- Run the program, open a browser and access http://localhost:8080/hello.
Image may be NSFW.
Clik here to view.
That’s it. You now have a self-hosted ASP.NET Web API server.
Image may be NSFW.
Clik here to view.

Clik here to view.
