Created
February 15, 2017 01:00
-
-
Save kflu/32e0ec23eb8a57294fa2ab1e5bd33869 to your computer and use it in GitHub Desktop.
Vanilla self host ASP.NET web API application
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
namespace Foo | |
{ | |
using System; | |
using System.Net; | |
using System.Threading; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Text; | |
using Newtonsoft.Json; | |
using System.Web.Http; | |
using System.Web.Http.SelfHost; | |
/// <summary> | |
/// Main class | |
/// </summary> | |
public static class MainClass | |
{ | |
static void Main(string[] args) | |
{ | |
var addr = new Uri("http://localhost:8001"); | |
using (var config = new HttpSelfHostConfiguration(addr)) | |
{ | |
config.Routes.MapHttpRoute("default", "api/{controller}"); | |
using (var srv = new HttpSelfHostServer(config)) | |
{ | |
srv.OpenAsync().Wait(); | |
Console.WriteLine("Server started"); | |
Console.ReadLine(); | |
Console.WriteLine("done"); | |
} | |
} | |
} | |
} | |
public class ProductController : ApiController | |
{ | |
public class Result | |
{ | |
public int Code; | |
public object Metadata; | |
} | |
[HttpPost] | |
public Result Post([FromBody]int[] data) | |
{ | |
return new Result | |
{ | |
Code = 1, | |
Metadata = data, | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment