【发布时间】:2013-08-09 15:29:18
【问题描述】:
我正在尝试使用 Service Stack 实现 Swagger。我已经使用 nuget 大摇大摆地安装了服务堆栈。当前的 DLL 版本大多报告为 3.9.56.0。
我正在尝试遵循...提供的示例 https://github.com/ServiceStack/ServiceStack.UseCases/tree/master/SwaggerHelloWorld
而且说明看起来很简单......
Plugins.Add(new ServiceStack.Api.Swagger.SwaggerFeature());
在我通过 nuget 安装后进入“配置”方法(如文档所示),然后我添加了 [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...
堆栈跟踪@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
此外,“请指定协议”是一个招摇的错误,显示在屏幕上上述“更改发现 url 的文本框”下方的 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”位,现在一切正常。
【问题讨论】:
-
您在 index.html 文件的
window.swaggerUi = new SwaggerUi....初始化脚本中为discoveryUrl设置的值是多少? -
我正在使用
../api,但我在屏幕上还有一个文本框可以更改该值并再次尝试。 @esker -
您能否显示“未找到请求的处理程序”错误的堆栈跟踪?另外,“请指定协议”-该错误究竟是如何出现的,您知道该错误来自何处吗?好像是Javascript错误等等
-
另外,您说“当前的 DLL 版本报告为 3.9.56.0大部分”。如果您的某些 ServiceStack 程序集引用的版本号不完全相同,则可能会出现问题。
-
感谢@esker 我已添加更新以回答您的问题
标签: c# servicestack swagger