Web API使用记录系列(三)Web API与Owin [ASP.NET] 下一代ASP.NET开发规范:OWIN

  还好在坚持,今天继续更新第三篇随笔----使用owin来启动WebAPI(这里还是以IIS为宿主,当然也可以使用别的如Console、Windows Server等)

  关于OWIN(Open Web Server Interface for .NET),.Net Web开发架构,在.NET Web Servers与Web Application之间定义了一套标准接口,目标是用于解耦Web Server和Web Application.

  关于详细讲解,可阅读 Never、C的这篇文章  [ASP.NET] 下一代ASP.NET开发规范:OWIN

  我们这里使用OWIN是为了下一节的OAuth做准备,记录的可能简单一些,后续有机会再单开一篇随笔记录下。

  1.安装引用

  Owin

  Microsoft.AspNet.WebApi.Owin

  Microsoft.Owin.Host.SystemWeb

  Microsoft.Owin.Cors

  安装Microsoft.AspNet.WebApi.Owin的时候,指定下版本,保持与WebApi的版本相同。否则最新的版本会安装响应的依赖,造成升级了api相关dll的版本

  2.根目录创建StartUp文件并配置如下

 1 [assembly: OwinStartup(typeof(SampleAPI.StartUp))]
 2 namespace SampleAPI
 3 {
 4     public class StartUp
 5     {
 6         public void Configuration(IAppBuilder app)
 7         {
 8             app.UseCors(CorsOptions.AllowAll);
 9 
10             HttpConfiguration config = GlobalConfiguration.Configuration;
11             GlobalConfiguration.Configure(WebApiConfig.Register);
12             app.UseWebApi(config);
13         }
14     }
15 }

  注:

  ①程序使用OWIN后,配置OWIN启动

   》使用OwinStartup特性,如上述代码中添加  [assembly: OwinStartup(typeof(SampleAPI.StartUp))] 

   》添加OIWIN启动发现 <add key="owin:AutomaticAppStartup" value="true"></add>    (测试好像不加也可以,禁用的时候可以使用)

  ②StartUp中使用 HttpConfiguration需要用GlobalConfiguration.Configuration,否则HelpPage页面的api列表将获取不到

   参考HelpPage原理:https://www.cnblogs.com/gdnyfcuso/p/8308357.html

  3.使用owin后HelpPage与Home无法访问处理(如果使用OWIN后未出现该问题的可以不配置)

  使用Owin后造成HelpPage与Home的无法访问或HelpPage页面API接口列表不显示的处理如下

  安装Owin.Extensions

  在App_Start中添加类OwinExtend,内容如下:

  

 1 public static class OwinExtend
 2     {
 3         internal static void UseWebApiAndHelp(this IAppBuilder app, HttpConfiguration config)
 4         {
 5             // WepApiStartup.Configure(config);
 6 
 7             app.UseHandlerAsync((request, response, next) =>
 8             {
 9                 if (request.Path == "/") //app.Map using a regex exclude list would be better here so it doesn't fire for every request
10                 {
11                     response.StatusCode = 301;
12                     response.SetHeader("Location", "/Help");
13                     return Task.FromResult(0);
14                 }
15 
16                 return next();
17             });
18 
19             // Map the help path and force next to be invoked
20             app.Map("/help", appbuilder => appbuilder.UseHandlerAsync((request, response, next) => next()));
21             app.Map("/home", appbuilder => appbuilder.UseHandlerAsync((request, response, next) => next()));
22 
23             app.UseWebApi(config);
24 
25         }
26     }

 修改StartUp文件中的app.UseWebApi(config)为app.UseWebApiAndHelp(config); 

 参考:https://stackoverflow.com/questions/18921215/cant-get-asp-net-web-api-2-help-pages-working-when-using-owin

  

  

  4.异常记录

   ①The following errors occurred while attempting to load the app.- No assembly found containing an OwinStartupAttribute.

    》是否添加StartUp文件》是否使用OwinStartup特性》是否启用OWIN启动发现

  ②没有 OWIN 身份验证管理器与此请求相关联

  https://www.cnblogs.com/OpenCoder/p/6900704.html

  本节内容记录到此,有用到的再补充,有不对的地方希望大家帮忙指正修改,感谢!

猜你喜欢

转载自www.cnblogs.com/xihao/p/10625043.html