如何将Swashbuckle Swagger API文档添加到带有.NET后端的Azure移动服务中?
答案 0 :(得分:0)
UPDATE:以下答案需要以下mod才能在部署到Azure后启用XML注释: 1)通过从bin文件夹复制到其他地方并包含在项目中,使生成的XML注释成为项目的一部分 2)XML注释嵌入式资源 3)将XML注释资源作为自定义资产添加到Swagger,如下所示:
c.CustomAsset("commentsxml", thisAssembly , "YOURSERVICE.YOURSUBFOLDER.YOURFILE.xml");
4)更改GetXmlCommentsPath方法,如下所示:
public static string GetXmlCommentsPath()
{
return WebConfigurationManager.AppSettings["XMLCommentsLocation"];
}
5)向web.config添加新自定义资产的完整http网址,例如:http://yoursite:yourport/swagger/ui/commentsxml
<强>更新强> 以下不适用于表控制器。仅适用于自定义控制器AFAICS
安装Swashbuckle Nuget包后:
Install-Package Swashbuckle
进入现有的Azure移动服务服务,诀窍是从SwaggerConfig中删除或注释掉以下内容
public class SwaggerConfig
{
public static void Register()
{
//var thisAssembly = typeof(SwaggerConfig).Assembly;
//GlobalConfiguration.Configuration
// .EnableSwagger(c =>
// {
// ...
并将.EnableSwagger ...添加到移动服务的初始化代码返回的配置对象中:
public static class WebApiConfig
{
public static string GetXmlCommentsPath()
{
return System.String.Format(@"{0}\bin\YOURSERVICENAME.XML", System.AppDomain.CurrentDomain.BaseDirectory);
}
public static void Register()
{
// Use this class to set configuration options for your mobile service
ConfigOptions options = new ConfigOptions();
options.LoginProviders.Add(typeof(CustomLoginProvider)); //YOU WILL NOT HAVE THIS IF YOU HAVEN'T ADDED IT
// Use this class to set WebAPI configuration options
HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));
config.SetIsHosted(true);
// To display errors in the browser during development, uncomment the following
// line. Comment it out again when you deploy your service for production use.
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include; //NOR THIS
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include; //NOR THIS
var thisAssembly = typeof(SwaggerConfig).Assembly;
// GlobalConfiguration.Configuration
config
.EnableSwagger(c =>
{...
答案 1 :(得分:0)
这对我有用
这会将该文件推送到Azure并解决此问题。您可以在项目中包含该文件(右键单击=&gt;包含在项目中),这样每次部署时都会将其推送到Azure。