c# webapi 配置swagger的方法
作者:团队buff工具人 时间:2022-03-21 23:38:19
如何配置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,安了有可能会报错
来源:https://www.cnblogs.com/aoximin/p/13195735.html
标签:c#,webapi,配置,swagger
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java获取任意http网页源代码的方法
2022-07-06 01:45:37
Android自定义View实现自动转圈效果
2021-11-11 21:01:48
C#中out与ref的区别实例解析
2022-01-27 13:29:09
Springboot创建子父工程过程图解
2022-09-20 06:06:26
![](https://img.aspxhome.com/file/2023/6/77496_0s.png)
C# Chart 简单使用教程
2021-05-29 07:41:34
![](https://img.aspxhome.com/file/2023/4/104234_0s.png)
C语言实现线性表的基本操作详解
2022-02-24 22:08:32
![](https://img.aspxhome.com/file/2023/4/120794_0s.png)
java合并多个文件的实例代码
2023-07-28 12:56:02
Spring boot实现文件上传功能
2023-08-01 07:00:02
通过实例解析传统jar包引用方式
2022-08-01 05:14:06
C#实现贪吃蛇小游戏
2022-09-28 17:17:59
![](https://img.aspxhome.com/file/2023/6/80116_0s.jpg)
mybatis中映射文件(mapper)中的使用规则
2021-06-28 20:09:08
![](https://img.aspxhome.com/file/2023/8/76248_0s.png)
关于Spring中的三级缓存解析
2022-08-20 15:31:06
![](https://img.aspxhome.com/file/2023/5/122965_0s.jpg)
配置SpringBoot方便的切换jar和war的方法示例
2023-11-22 11:57:54
swing分割窗口控件JSplitPane使用方法详解
2021-07-28 14:15:20
spring cloud gateway网关路由分配代码实例解析
2021-06-09 02:54:08
Java编程中随机数的生成方式总结
2022-06-14 11:57:27
MyBatis-Plus实现多数据源的示例代码
2023-11-11 12:58:11
![](https://img.aspxhome.com/file/2023/3/59383_0s.png)
java去除字符串中的空格、回车、换行符、制表符的小例子
2022-04-13 12:33:44
C语言实现2D赛车游戏的示例代码
2022-04-03 05:57:33
![](https://img.aspxhome.com/file/2023/4/117794_0s.jpg)
C#开发Winform控件之打开文件对话框OpenFileDialog类
2023-04-19 10:53:16
![](https://img.aspxhome.com/file/2023/3/116513_0s.jpg)