首先创建一个空的web项目,如下图所示:
项目创建成功以后,安装下面三个package.
Install-Package Microsoft.AspNet.WebApi -Version 5.2.2
Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.2.2Install-Package Microsoft.Owin.Host.SystemWeb -Version 3.0.0
创建Owin Startup 类
1 using System; 2 using System.Threading.Tasks; 3 using Microsoft.Owin; 4 using Owin; 5 using System.Web.Http; 6 7 [assembly: OwinStartup(typeof(FirstOwinWebApi.Startup))] 8 9 namespace FirstOwinWebApi10 {11 public class Startup12 {13 public void Configuration(IAppBuilder app)14 {15 HttpConfiguration config = new HttpConfiguration();16 17 // Web API routes18 config.MapHttpAttributeRoutes();19 20 //WebApiConfig.Register(config);21 22 app.UseWebApi(config);23 }24 }25 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Net.Http; 6 using System.Web.Http; 7 8 namespace FirstOwinWebApi.Controllers 9 {10 [RoutePrefix("api/HelloWorld")]11 public class HelloWorldController : ApiController12 {13 [Route("")]14 public IHttpActionResult Post()15 {16 17 return Ok("Hello World");18 19 }20 21 }22 }
F5运行,使用Postman访问Api
其他一些Owin中间件包:
Install-Package Microsoft.AspNet.WebApi -Version 5.2.2
Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.2.2
Install-Package Microsoft.Owin.Host.SystemWeb -Version 3.0.0
Install-Package Microsoft.Owin.Cors -Version 3.0.0
Install-Package Microsoft.Owin.Security.OAuth -Version 3.0.0
Install-Package Microsoft.Owin.Security.Jwt -Version 3.0.0
Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.0
Install-Package Thinktecture.IdentityModel.Core Version 1.2.0
Install-Package Microsoft.AspNet.Identity.Owin -Version 2.0.1
Install-Package Microsoft.AspNet.Identity.EntityFramework -Version 2.0.1