首先创建一个空的web项目,如下图所示:
项目创建成功以后,安装下面三个package.
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
创建Owin Startup 类
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using System.Web.Http; [assembly: OwinStartup(typeof(FirstOwinWebApi.Startup))] namespace FirstOwinWebApi
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration(); // Web API routes
config.MapHttpAttributeRoutes(); //WebApiConfig.Register(config); app.UseWebApi(config);
}
}
}
创建API Controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http; namespace FirstOwinWebApi.Controllers
{
[RoutePrefix("api/HelloWorld")]
public class HelloWorldController : ApiController
{
[Route("")]
public IHttpActionResult Post()
{ return Ok<string>("Hello World"); } }
}
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