如何在WebAPI中将Swagger用作IAppBuilder的欢迎页面 [英] How to use Swagger as Welcome Page of IAppBuilder in WebAPI

查看:217
本文介绍了如何在WebAPI中将Swagger用作IAppBuilder的欢迎页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 Swagger 用于Microsoft WebAPI 2.

I try to use Swagger with Microsoft WebAPI 2.

目前,我在方法中进行了以下调用.

For the moment, I've the following call in a method.

appBuilder
   .ConfigureOAuth()
   .UseWebApi(configuration)
   .UseWelcomePage();

如果我想使用Swagger,则必须使用此网址" https://localhost:44300/swagger ",该网址非常有效好吧.

If I want to use Swagger, I must use this url "https://localhost:44300/swagger" which one works very well.

我希望主页重定向到我的招摇网址,如下所示,但此示例不起作用.

I want my home page redirects to the url of my swagger, perhaps as follows but this sample doesn't works.

    appBuilder
       ...
       .UseWelcomePage("/swagger");

有什么主意吗?

推荐答案

我通过在RouteConfig.cs中添加一条路由来实现所需的工作,如下所示:

I got this working how I wanted by adding a route in RouteConfig.cs like so:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "swagger_root", 
            routeTemplate: "", 
            defaults: null, 
            constraints: null,
            handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

从swashbuckle看到以下代码以查看发生了什么: https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Application/RedirectHandler.cs

See this code from swashbuckle to see what's going on: https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Application/RedirectHandler.cs

这篇关于如何在WebAPI中将Swagger用作IAppBuilder的欢迎页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆