ServiceStack:使用自托管应用程序重写URL

时间:2014-05-12 12:40:51

标签: servicestack servicestack-bsd

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

注意

我使用的是ServiceStack V3

1 个答案:

答案 0 :(得分:2)

ServiceStack v4

在ServiceStack v4中,我使用原始的http处理程序拦截root。在您的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; }
    }
}

希望有所帮助。