我已经编写了一个asp web-api,我使用this class为api添加了版本。
我的路线看起来像这样:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{namespace}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));
所以我可以访问https://localhost:44301/v1/test
我现在正在尝试添加Swagger来生成一些api文档(实际上我正在使用Swashbuckle Nuget包)
当我浏览到/swagger
时,页面加载但是所有控制器都出错:
Unable to read api 'TestController' from path https://localhost:44301/swagger/api-docs/TestController (server returned undefined)
如果我浏览https://localhost:44301/swagger/api-docs/TestController
,它会出现一个NullReferenceException
,其堆栈跟踪为:
at Swashbuckle.Swagger.OperationGenerator.CreateParameter(ApiParameterDescription apiParamDesc, String apiPath)
at Swashbuckle.Swagger.OperationGenerator.<>c__DisplayClass2.<ApiDescriptionToOperation>b__1(ApiParameterDescription paramDesc)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Swashbuckle.Swagger.OperationGenerator.ApiDescriptionToOperation(ApiDescription apiDescription)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Swashbuckle.Swagger.ApiExplorerAdapter.CreateApi(IGrouping`2 apiDescriptionGroup, OperationGenerator operationGenerator)
at Swashbuckle.Swagger.ApiExplorerAdapter.<>c__DisplayClassb.<GetDeclaration>b__7(IGrouping`2 apiDescGrp)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Swashbuckle.Swagger.ApiExplorerAdapter.GetDeclaration(String basePath, String version, String resourceName)
at Swashbuckle.Application.CachingSwaggerProvider.<>c__DisplayClass4.<GetDeclaration>b__3(String k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Swashbuckle.Application.CachingSwaggerProvider.GetDeclaration(String basePath, String version, String resourceName)
at Swashbuckle.Application.SwaggerSpecHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Web.Http.HttpServer.<>n__FabricatedMethod9(HttpRequestMessage , CancellationToken )
at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
如果我改变路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
)
然后Swagger页面正常工作,但实际的api却没有!
所以问题是,如何让Swagger使用版本化的api?
答案 0 :(得分:1)
最后我使用了SDammann.WebApi.Versioning
nuget包和以下路由:
VersionedControllerSelector.VersionPrefix = "v";
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "v{version}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我的控制器位于名为v1或v2等的命名空间中。
答案 1 :(得分:-1)
试试这个
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
//defaults: new { id = RouteParameter.Optional }
);