OWIN入门初探

  关于OWIN的一个入门程序,自己写一个简单的.NET Web Server,

  1、创建一个Console控制台程序,.NET版本在4.5及以上

  

  2、新建文件Startup.cs,这个文件主要负责配置.NET OWIN的初始化参数

  

 1 using Microsoft.Owin;
 2 using Owin;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace Console_OWIN_Application
10 {
11     public class Startup
12     {
13         public void Configuration(IAppBuilder appBuilder)
14         {
15             appBuilder.Run(HandleRequest);
16         }
17 
18         static Task HandleRequest(IOwinContext context)
19         {
20             context.Response.ContentType = "text/plain";
21             return context.Response.WriteAsync("Hello World");
22         }
23     }
24 }

  

  

  3、具体执行Web Server, 在Program.cs文件中添加代码如下:

  

 1 using Microsoft.Owin.Hosting;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Console_OWIN_Application
 9 {
10     public class Program
11     {
12         static void Main(string[] args)
13         {
14             var url = "http://localhost:8088";
15             var startOpts = new StartOptions(url)
16             {
17 
18             };
19 
20             using (WebApp.Start<Startup>(startOpts))
21             {
22                 Console.WriteLine("Server run at " + url + ", press Enter to exit.");
23                 Console.ReadLine();
24             }
25         }
26     }
27 }

  

  4、最后,运行结果如下图:

  

  

  

原文地址:https://www.cnblogs.com/zhoulingxiang/p/6541236.html