ServiceStack全局请求重定向

时间:2017-09-27 00:11:28

标签: c# redirect servicestack swagger-ui

我正在尝试重定向以使用/ docs而不是/ swagger-ui。我实现了处理程序(基本上从OpenApiFeature.cs复制)以捕获/ docs路径和server / swagger-ui内容,但它只适用于/ docs /,而不是/ docs。使用/ docs,所有css和js文件都解析为root而不是/ docs文件夹(host / blah.js而不是host / docs / blah.js):

appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) =>
{
    IVirtualFile indexFile = null;

    switch (pathInfo.ToLower())
    {
        case "/docs":
        case "/docs/":
        case "/docs/default.html":
            indexFile = appHost.VirtualFileSources.GetFile("/swagger-ui/index.html");
            break;
        default:
            indexFile = null;
            break;
    }

    if (indexFile != null)
    {
        var html = indexFile.ReadAllText();

        return new CustomResponseHandler((req, res) =>
        {
            res.ContentType = MimeTypes.Html;
            return html;
        });
    }
    return pathInfo.StartsWithIgnoreCase("/docs") ? new StaticFileHandler(pathInfo.ReplaceFirst("/docs", "/swagger-ui")) : null;
});

要解决这个问题,我想我可以将/ docs重定向到/ docs /:

        case "/docs":
        return new RedirectHttpHandler() {RelativeUrl = "/docs/"};

问题在于它还以某种方式将连接从https降级为http。由于它是相对重定向,我不确定那里发生了什么。有没有更好的方法来实现这个?更糟糕的是,它适用于我的本地开发机器,但不适用于我们的测试环境。两者都运行独立的SS。显然有一些不同的设置,但我无法想象会导致什么。

我可能错过了一些愚蠢的东西,但我找不到它。是否有更新的设置,以使这更容易开箱即用?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

ServiceStack将重写网址以包含httphttps,具体取决于您可以通过设置https强制使用Config.UseHttpsLinks=true链接的传入请求,这也是最简单的方法添加重定向是将其添加到Config.RedirectPaths,这看起来像:

SetConfig(new HostConfig {
    UseHttpsLinks = true,
    RedirectPaths = {
        { "/docs", "/swagger-ui/" },
    }
});

注意:如果您使用终止https SSL连接的代理,理想情况下应将其配置为设置X-Forwarded-Proto: https HTTP请求标头,以便下游App服务器可以确定原始请求是通过https发送的。