我正在尝试使用Service Stack实现Swagger。我使用nuget安装了swagger服务栈。当前的DLL版本主要报告为3.9.56.0。
我正在尝试按照...提供的示例 https://github.com/ServiceStack/ServiceStack.UseCases/tree/master/SwaggerHelloWorld
并且说明看起来相当傻瓜......
Plugins.Add(new ServiceStack.Api.Swagger.SwaggerFeature());
通过nuget安装后,进入'Configure'方法(如文档指示),然后我添加了[ApiMember]和[Api]标签,以及对[Route]标签的更改以添加摘要和笔记
但是当我访问~/swagger-ui/index.html
时,我收到了错误
Please specify the protocol for ../api
我的api位于~/api
,我现在只有一个方法(Hello World)坐在~api/Hello/{name}
,返回JSON并正常工作。
如果我访问~api
,我会收到带有堆栈跟踪类型输出的消息Handler for Request not found:
。
我做错了什么?启用swagger的说明看起来非常简单,并且似乎缺少详细的说明,可能是因为它应该“正常工作”,请帮忙!
更新以解决Esker ...
Stack trace @ myhost:54011 / api
Handler for Request not found:
Request.ApplicationPath: /
Request.CurrentExecutionFilePath: /api
Request.FilePath: /api
Request.HttpMethod: GET
Request.MapPath('~'): D:\Starteam\Private\user\web\ServiceStackSwagger\ServiceStackSwagger\
Request.Path: /api
Request.PathInfo:
Request.ResolvedPathInfo: /api
Request.PhysicalPath: D:\Starteam\Private\user\web\ServiceStackSwagger\ServiceStackSwagger\api
Request.PhysicalApplicationPath: D:\Starteam\Private\user\web\ServiceStackSwagger\ServiceStackSwagger\
Request.QueryString:
Request.RawUrl: /api
Request.Url.AbsoluteUri: http://localhost:54011/api
Request.Url.AbsolutePath: /api
Request.Url.Fragment:
Request.Url.Host: localhost
Request.Url.LocalPath: /api
Request.Url.Port: 54011
Request.Url.Query:
Request.Url.Scheme: http
Request.Url.Segments: System.String[]
App.IsIntegratedPipeline: True
App.WebHostPhysicalPath: D:\Starteam\Private\user\web\ServiceStackSwagger\ServiceStackSwagger
App.WebHostRootFileNames: [global.asax,global.asax.cs,helloservice.cs,jquery-1.10.2.js,packages.config,servicestackswagger.csproj,servicestackswagger.csproj.user,web.config,web.debug.config,web.release.config,app_data,app_start,bin,controllers,dto,models,obj,properties,swagger-ui,views]
App.DefaultHandler: metadata
App.DebugLastHandlerArgs: GET|/api|D:\Starteam\Private\user\web\ServiceStackSwagger\ServiceStackSwagger\api
另外,“请指定协议”是一个招摇错误,在上述'文本框下面的html中显示在屏幕上以更改发现网址
并且DLL版本“大部分”相同,因为ServiceStack.Redis是版本3.9.57.0,但我没有使用它,所以'大多数'
更新...我的解决方案
我在<configuration>
标记内的web.config文件中需要这个,我之前没有包含<location path="api">
位。
<!-- ServiceStack: CustomPath /api -->
<location path="api">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*"/>
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</location>
也......我已经设置了我的“路线”......
//INCORRECT
[Route("/api/Hello/{name}", Summary = @"N031'5 ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes")]
public class Hello : IReturn<HelloResponse>
但这与swagger的功能相混淆,需要......
//CORRECT
[Route("/Hello/{name}"...
将'api'位删除,现在一切正常。
答案 0 :(得分:2)
您需要告诉Swagger有关ServiceStack Swagger端点的特定URL,而不是服务的基本URL。此端点位于基本网址下的/resources
。因此,../api
代替discoveryUrl
,请在index.html文件中使用../api/resources
,如下所示:
window.swaggerUi = new SwaggerUi({
discoveryUrl: '../api/resources',
...