gpt4 book ai didi

asp.net-web-api2 - 使用 swashbucle 启用 swagger 时出错

转载 作者:行者123 更新时间:2023-12-05 08:19:04 25 4
gpt4 key购买 nike

我正在尝试使用几个简单的 Controller 创建一个 WebApi 项目。如果我使用 fiddler 调用方法,一切都很好,但我更喜欢使用 swashbuckle,因为它更漂亮一些。

但是,使用默认配置安装 swashbuckle 后它无法正常工作。

当我导航到 http://localhost/api/mycontroller/swagger

它重定向到 http://localhost/api/mycontroller/swagger/ui/index

但是它只是显示以下错误:

<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost/api/Management/swagger/ui/index'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'swagger'. </MessageDetail>
</Error>

我的路由如下: 配置.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

最佳答案

我们找到了一个非常简单的解决方案来解决我们的问题。

当 swagger 添加到项目中时,它会自动更新 App_Start 中的 Bootstrapper.cs,它看起来像这样:

    WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
SwaggerConfig.Register(config);

只需将 swagger 移动到列表的顶部就可以让它在 web api 路由之前正确注册它的路由:

    SwaggerConfig.Register(config); // Swagger must be the first thing in this sequence or it just does not work...
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);

只是张贴这个,希望它能帮助别人。

关于asp.net-web-api2 - 使用 swashbucle 启用 swagger 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904810/

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