gpt4 book ai didi

c# - Swagger 的服务堆栈不起作用

转载 作者:太空狗 更新时间:2023-10-29 23:39:27 27 4
gpt4 key购买 nike

我正在尝试使用服务堆栈实现 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 安装后进入“配置”方法(如文档指示),然后我添加了 [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

此外,“请指定协议(protocol)”是一个 Swagger 的错误,以 html 格式显示在屏幕上上述“用于更改发现 URL 的文本框”下方

并且 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”位,现在一切正常。

最佳答案

您需要将 ServiceStack Swagger 端点的特定 URL 告诉 Swagger,而不是您的服务的基本 URL。此端点位于您的基本 URL 下的 /resources。因此,对于 discoveryUrl,不要使用 ../api,而是像这样在 index.html 文件中使用 ../api/resources :

window.swaggerUi = new SwaggerUi({
discoveryUrl: '../api/resources',
...

关于c# - Swagger 的服务堆栈不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18150351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com