C# 如何配置Swashback以显示api版本而不是v{version}变量?
我正在使用Swashback来记录我的Web API 2.2 API。当我加载Swagger页面时,uri显示为版本占位符变量,而不是实际版本。例如:C# 如何配置Swashback以显示api版本而不是v{version}变量?,c#,.net,asp.net-web-api,swagger,swashbuckle,C#,.net,Asp.net Web Api,Swagger,Swashbuckle,我正在使用Swashback来记录我的Web API 2.2 API。当我加载Swagger页面时,uri显示为版本占位符变量,而不是实际版本。例如: /api/v{version}/authentication 而不是: /api/v2/authentication 我如何配置我的应用程序或Swashback以显示版本号而不是版本变量?抱歉,刚才注意到您正在谈论URI…不确定下面是否有帮助 您是否在您的招摇过市配置中尝试过以下内容: public static void Register(
/api/v{version}/authentication
而不是:
/api/v2/authentication
我如何配置我的应用程序或Swashback以显示版本号而不是版本变量?抱歉,刚才注意到您正在谈论URI…不确定下面是否有帮助 您是否在您的招摇过市配置中尝试过以下内容:
public static void Register(HttpConfiguration config)
{
config
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "version api");
c.PrettyPrint();
c.OAuth2("oauth2").Description("OAuth2 ResourceOwner Grant").TokenUrl("/testtoken");
c.IncludeXmlComments(GetXmlCommentsPath());
c.DocumentFilter<AuthTokenOperation>();
c.DocumentFilter<ListManagementSwagger>();
c.SchemaFilter<SchemaExamples>();
})
.EnableSwaggerUi(c =>
{
c.DocumentTitle("test webapi");
});
}
公共静态无效寄存器(HttpConfiguration配置)
{
配置
.EnableSwagger(c=>
{
c、 单一api版本(“v1”,“版本api”);
c、 预打印();
c、 OAuth2(“OAuth2”).Description(“OAuth2资源所有者授权”).TokenUrl(“/testtoken”);
c、 includexmlcoments(getxmlcomentspath());
c、 DocumentFilter();
c、 DocumentFilter();
c、 SchemaFilter();
})
.EnableSwaggerUi(c=>
{
c、 文档标题(“测试webapi”);
});
}
更新了WebApiConfig的代码:
// Web API configuration and services
var constraintResolver = new System.Web.Http.Routing.DefaultInlineConstraintResolver()
{
ConstraintMap =
{
["apiVersion"] = typeof(Microsoft.Web.Http.Routing.ApiVersionRouteConstraint)
}
};
config.AddVersionedApiExplorer(opt =>
{
opt.SubstituteApiVersionInUrl = true;
});
config.MapHttpAttributeRoutes(constraintResolver);
config.AddApiVersioning();
// Web API routes
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
一些参考资料这是实现版本控制的方法之一。我有一个自定义头和自定义根url函数,您可以忽略这一部分。这段代码要求Swagger根据提供的xml构建两个不同的版本
public class SwaggerConfig
{
public static void Register()
{
var customHeader = new SwaggerHeader //you can ignore this one
{
Description = "Custom header description",
Key = "customHeaderId",
Name = "customHeaderId"
};
var versionSupportResolver = new Func<ApiDescription, string, bool>((apiDescription, version) =>
{
var path = apiDescription.RelativePath.Split('/');
var pathVersion = path[1];
return string.Equals(pathVersion, version, StringComparison.OrdinalIgnoreCase);
});
var versionInfoBuilder = new Action<VersionInfoBuilder>(info => {
info.Version("v2", "My API v2");
info.Version("v1", "My API v1");
});
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
//c.RootUrl(ComputeHostAsSeenByOriginalClient); //you can ignore this custom function
c.Schemes(new[] { "http", "https" });
customHeader.Apply(c);
c.MultipleApiVersions(versionSupportResolver, versionInfoBuilder);
c.IgnoreObsoleteActions();
c.IncludeXmlComments(GetXmlCommentsPath());
c.DescribeAllEnumsAsStrings();
})
.EnableSwaggerUi("swagger/ui/{*assetPath}", c =>
{
c.DisableValidator();
c.SupportedSubmitMethods("GET", "POST");
});
}
private static Func<XPathDocument> GetXmlCommentsPath()
{
return () =>
{
var xapixml = GetXDocument("My.API.xml");
var xElement = xapixml.Element("doc");
XPathDocument xPath = null;
if (xElement != null)
{
using (var ms = new MemoryStream())
{
var xws = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false };
using (var xw = XmlWriter.Create(ms, xws))
{
xElement.WriteTo(xw);
}
ms.Position = 0;
xPath = new XPathDocument(ms);
}
}
return xPath;
};
}
private static XDocument GetXDocument(string file)
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");
var xDoc = XDocument.Load(path + "\\" + file);
return xDoc;
}
//ComputeHostAsSeenByOriginalClient function code
}
public类SwaggerConfig
{
公共静态无效寄存器()
{
var customHeader=new SwaggerHeader//您可以忽略此项
{
Description=“自定义标题说明”,
Key=“customHeaderId”,
Name=“customHeaderId”
};
var versionSupportResolver=新函数((apiDescription,version)=>
{
var path=apiscription.RelativePath.Split('/');
var pathVersion=路径[1];
返回string.Equals(pathVersion、version、StringComparison.OrdinalIgnoreCase);
});
var versionInfoBuilder=新操作(信息=>{
信息版本(“v2”、“我的API v2”);
信息版本(“v1”、“我的API v1”);
});
全局配置
.EnableSwagger(c=>
{
//c、 RootUrl(ComputeHostAsSeenByOriginalClient);//您可以忽略此自定义函数
c、 方案(新的[]{“http”,“https”});
customHeader.Apply(c);
c、 多版本(版本支持解析器、版本信息生成器);
c、 忽略过时操作();
c、 includexmlcoments(getxmlcomentspath());
c、 descripbeAllenumsasstrings();
})
.EnableSwaggerUi(“swagger/ui/{*assetPath}”,c=>
{
c、 禁用验证器();
c、 支持提交方法(“获取”、“发布”);
});
}
私有静态函数GetXmlCommentsPath()
{
return()=>
{
var xapixml=GetXDocument(“My.API.xml”);
var-xElement=xapixml.Element(“doc”);
XPathDocument xPath=null;
if(xElement!=null)
{
使用(var ms=new MemoryStream())
{
var xws=newXMLWriterSettings{OmitXmlDeclaration=true,Indent=false};
使用(var xw=XmlWriter.Create(ms,xws))
{
xElement.WriteTo(xw);
}
ms.Position=0;
xPath=新的XPathDocument(ms);
}
}
返回xPath;
};
}
私有静态XDocument GetXDocument(字符串文件)
{
var path=path.Combine(AppDomain.CurrentDomain.BaseDirectory,“bin”);
var xDoc=XDocument.Load(路径+“\\”+文件);
返回xDoc;
}
//ComputeHostAsSeenByOriginalClient函数代码
}
我没有使用asp.net coreGood!如果您指出了该代码,那么人们会对您使用的代码有一个了解,那么会容易得多。无论如何,我已经将我的代码更新为您正在使用的ASP.Net的正确类型。仅凭这几个细节很难提供准确的答案@您能否提供一个复制您的问题的示例项目