Created
September 20, 2013 08:06
-
-
Save joeriks/6634580 to your computer and use it in GitHub Desktop.
One file simple usage WebApi selfhost (console program + Microsoft.AspNet.WebApi.SelfHost nuget). Needs to be run as admin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web.Http.SelfHost; | |
using System.Web.Http; | |
namespace ConsoleApplication1 | |
{ | |
// listens to /api/foo/{id} | |
public class FooController : System.Web.Http.ApiController | |
{ | |
public string Get(int id) | |
{ | |
return "Foo " + id; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = new HttpSelfHostConfiguration("http://localhost:8087"); | |
config.Routes.MapHttpRoute( | |
"API Default", "api/{controller}/{id}", | |
new { id = RouteParameter.Optional }); | |
using (HttpSelfHostServer server = new HttpSelfHostServer(config)) | |
{ | |
server.OpenAsync().Wait(); | |
Console.WriteLine("Press Enter to quit."); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code.
If you want to run a self-hosted API without admin permissions, for example when running a test, use the following:
And access it via http://localhost/Temporary_Listen_Addresses/Demo/api/Foo/42.