c# webapi 配置swagger的方法

所属分类: 软件编程 / C#教程 阅读数: 145
收藏 0 赞 0 分享

如何配置swagger?

在使用项目中,我们希望去查看我们的webapi的测试,那么我们是需要去有一个集成的测试的。

步骤

1.在nutget管理包中下载swagger包。

2.这样会在App_start 文件夹中出现swaggerconfig.cs 和swaggerNet.cs,
这个时候就需要配置的时候了。

3.取消下面的注释(swaggerconfig.cs)

 c.IncludeXmlComments(string.Format("{0}/bin/ThinkingSpace.XML", System.AppDomain.CurrentDomain.BaseDirectory));

当然我们为了代码的模块化,可以封装到一个方法中:

private static string GetXmlCommentsPath()
{
  return $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\GetDocumentation.XML";
}

好吧,ok,我们知道了这个配置了。

那么我们需要再bin目录下创建一个xml,推荐是项目名.xml.

4.那么接下来就是swaggerNet.cs配置.

using System;
using System.IO;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;
using System.Web.Http.Dispatcher;
using System.Web.Routing;
using Swagger.Net;

[assembly: WebActivator.PreApplicationStartMethod(typeof(ThinkingSpace.App_Start.SwaggerNet), "PreStart")]
[assembly: WebActivator.PostApplicationStartMethod(typeof(ThinkingSpace.App_Start.SwaggerNet), "PostStart")]
namespace ThinkingSpace.App_Start 
{
  public static class SwaggerNet 
  {
    public static void PreStart() 
    {
      RouteTable.Routes.MapHttpRoute(
        name: "SwaggerApi",
        routeTemplate: "api/docs/{controller}",
        defaults: new { swagger = true }
      );      
    }
    
    public static void PostStart() 
    {
      var config = GlobalConfiguration.Configuration;

      config.Filters.Add(new SwaggerActionFilter());
      
      try
      {
        config.Services.Replace(typeof(IDocumentationProvider),
          new XmlCommentDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/ThinkingSpace.XML")));
      }
      catch (FileNotFoundException)
      {
        throw new Exception("Please enable \"XML documentation file\" in project properties with default (bin\\ThinkingSpace.XML) value or edit value in App_Start\\SwaggerNet.cs");
      }
    }
  }
}

统一我们需要修改xml的位置即可。

注意

我们需要在webapi中只能存在一个get,否则会报错,因为需要符合restful 标准。

一个controller中只能有一个HttpGet请求,多了就会报错。建议减少重载方法,将其他Get方法分开

如果在swagger.config中加上c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());则会只显示第一个get方法

另:可以不安装swagger ui for .net,安了有可能会报错

以上就是c# webapi 配置swagger的方法的详细内容,更多关于c# 配置swagger的资料请关注脚本之家其它相关文章!

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多