简体   繁体   English

ServiceStack全局请求重定向

[英]ServiceStack global request redirect

I'm trying to get a redirect to work to use /docs instead of /swagger-ui. 我正在尝试重定向以使用/ docs而不是/ swagger-ui。 I implemented the handler (basically copied from OpenApiFeature.cs ) to catch the /docs path and server /swagger-ui content, but it only works with /docs/, not /docs. 我实现了处理程序(基本上从OpenApiFeature.cs复制)来捕获/ docs路径和服务器/ swagger-ui内容,但它仅适用于/ docs /,不适用于/ docs。 With /docs, all the css and js files resolve to the root instead of the /docs folder (host/blah.js instead of host/docs/blah.js): 使用/ docs,所有的css和js文件都解析为根目录,而不是/ 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;
});

To fix this, I figured I could just redirect /docs to /docs/: 为了解决这个问题,我认为我可以将/ docs重定向到/ docs /:

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

The problem with that is it also downgrades the connection from https to http somehow. 问题在于它也以某种方式将连接从https降级到http。 Since it's a relative redirect, I'm not sure what's going on there. 由于这是相对重定向,因此我不确定发生了什么。 Is there a better way to implement this? 有没有更好的方法来实现这一目标? To make it worse, it works on my local dev machine, but not our test environment. 更糟糕的是,它可以在我的本地开发机器上运行,但不能在我们的测试环境上运行。 Both run stand-alone SS. 两者都运行独立的SS。 Clearly there's some different setting, but I can't really imagine what would cause that. 显然有一些不同的设置,但是我真的无法想象会导致什么。

I'm probably missing something dumb, but I just can't find it. 我可能想念一些愚蠢的东西,但我只是找不到。 Is there a more recent setting to make this easier out-of-the-box? 是否有更新的设置可以使开箱即用变得更容易? Any ideas? 有任何想法吗?

ServiceStack will rewrite the url to include http or https depending on the incoming request which you can force to use https links by setting Config.UseHttpsLinks=true , also the easiest way to add a redirect is to add it to Config.RedirectPaths , together this looks like: ServiceStack将根据传入的请求重写URL以包括httphttps ,通过设置Config.UseHttpsLinks=true可以强制使用https链接,添加重定向的最简单方法是将其添加到Config.RedirectPaths中 ,看起来像:

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

Note: if you're using a proxy that's terminating the https SSL connection it should ideally be configured to set the X-Forwarded-Proto: https HTTP Request Header so downstream App servers can determine whether the original request was sent over https . 注意:如果您使用的是终止https SSL连接的代理,则理想情况下,应将其配置为设置X-Forwarded-Proto: https HTTP请求标头,以便下游App服务器可以确定原始请求是否通过https发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM