http://owin.org/
Owin defines the standard interface between Webserver and WebApplication, and the goal is to understand the dependence of webapplication on webserver,
This means that you can easily build a lightweight httpserver in the future,
1.Basic Sample Self Host
The following is a basic self Host http Server Via Owin, all of which is to get the client HTTP request, and then respond, when the unhandled exception occurs, will automatically jump to the error page,
STEP1:Install-Package Microsoft.Owin.SelfHost
Step2:configuration in Startup.css
Using system;using system.threading.tasks;using microsoft.owin;using owin;namespace owinselfhostbasic{public Class Startup {public void Configuration (Iappbuilder app) { app. Useerrorpage (); For more information about to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app. Run (context = { if (context. Request.Path.Value = = "/fail") { throw new Exception ("Random Exception"); } Context. Response.ContentType = "Text/plain"; Return to context. Response.writeasync ("Hello, World");}}}
App. Useerrorpage ();
Specify an error page for WebApplication,
App. Run Join response logic
STEP3: Specifying a listening port
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace owinselfhostbasic{ class program { static void Main (string[] args) { using ( Microsoft.owin.hosting.webapp.start<startup> ("http://localhost:9000")) { Console.WriteLine (" Press [Enter] to quit ... "); Console.ReadLine ();}}}
OK, everything is ready, start the console program, request Http://localhost:9000/This address, looks more convenient than Nodejs.
2.Self Host Web API using Owin
STEP1:Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
Step2:Startup.cs
Using owin;using System.Web.Http; namespace owinwebapiselfhost{public class Startup { //This code configures WEB API. The Startup class is specified as a type //parameter in the Webapp.start method. public void Configuration (Iappbuilder appBuilder) { //Configure Web API for self-host. httpconfiguration config = new httpconfiguration (); Config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} ); Appbuilder.usewebapi (config); } } }
Step3:apicontroller
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.web.http;namespace owinwebapiselfhost{public class Valuescontroller:apicontroller { // Get api/values public ienumerable<string> get () { return new string[] {"value1", "value2"}; } }}
STEP4: Setting the Listening port
Using microsoft.owin.hosting;using system;using system.net.http;namespace owinwebapiselfhost{Public class Program { static void Main () { string baseaddress = "Http://localhost:9000/"; Start OWIN host using (webapp.start<startup> (url:baseaddress)) { //Create httpcient and make a req Uest to api/values HttpClient client = new HttpClient (); var response = client. Getasync (baseaddress + "Api/values"). Result; Console.WriteLine (response); Console.WriteLine (Response. Content.readasstringasync (). Result); Console.ReadLine ();}}}
STEP5: Start the console program, here we see the return of the data is in JSON format, when we use Firefox browser request, we will find that the return is the XML format of the data, because Firefox default accept XML
Owin Self Host