【问题标题】:ServiceStack: URL Re-writing with Self-Hosted applicationServiceStack:使用自托管应用程序重写 URL
【发布时间】:2014-05-12 12:40:51
【问题描述】:

我有一个自托管应用程序,它的根目录有一个index.html 文件。当我运行应用程序并转到localhost:8090(应用程序托管在此端口上)时,URL 看起来像:http://localhost:8090/index.html。无论如何我可以在 index.html 页面上将 URL 设为:http://localhost:8090

注意

我正在使用 ServiceStack V3

【问题讨论】:

    标签: servicestack servicestack-bsd


    【解决方案1】:

    ServiceStack v4

    在 ServiceStack v4 中,我使用原始 http 处理程序来拦截根。在您的 AppHost Configure 方法中:

    public override void Configure(Container container)
    {
        var handleRoot = new CustomActionHandler((httpReq, httpRes) => {
            httpRes.ContentType = "text/html";
            httpRes.WriteFile("index.html");
            httpRes.End();
        });
    
        RawHttpHandlers.Add(httpReq => (httpReq.RawUrl == "/") ? handleRoot : null);
    }
    

    ServiceStack v3

    在 ServiceStack v3 中,您可以做类似的事情,但您必须自己包含 CustomActionHandler 类。所以在你的配置方法中:

    public override void Configure(Container container)
    {
        var handleRoot = new CustomActionHandler((httpReq, httpRes) => {
            httpRes.ContentType = "text/html";
            httpRes.WriteFile("index.html");
            httpRes.End();
        });
    
        SetConfig(new EndpointHostConfig {
            RawHttpHandlers = { httpReq => (httpReq.RawUrl == "/") ? handleRoot : null  },
        });
    }
    

    CustomActionHandler提供by Mythz here

    public class CustomActionHandler : IServiceStackHttpHandler, IHttpHandler 
    {
        public Action<IHttpRequest, IHttpResponse> Action { get; set; }
    
        public CustomActionHandler(Action<IHttpRequest, IHttpResponse> action)
        {
            if (action == null)
                throw new Exception("Action was not supplied to ActionHandler");
    
            Action = action;
        }
    
        public void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {            
            Action(httpReq, httpRes);
        }
    
        public void ProcessRequest(HttpContext context)
        {
            ProcessRequest(context.Request.ToRequest(GetType().Name), 
                context.Response.ToResponse(),
                GetType().Name);
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    希望对您有所帮助。

    【讨论】:

    • 啊,我还在用V3。我应该提到这一点
    • @BiffBaffBoff 我添加了一个 v3 实现。它有点长,因为它要求您自己实现CustomActionHandler(但这只是按原样复制和粘贴)。但它本质上与 v4 相同(除了 v4 已经提供了这个类)。希望对您有所帮助。
    • 感谢@Scott!我也不知道有一个 V3 特定的 SS 标签 :) 我明天会测试它,因为我现在没有代码
    • @BiffBaffBoff 酷。是的was introduced last December,但大多数人不知道。所以我通常在 v3 建立后进行标记。
    • 嗨@Scott。这似乎将 html 页面作为原始文本而不是 text/html 返回?
    猜你喜欢
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2023-03-06
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多