0

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

笔记

我正在使用 ServiceStack 的 V3

4

1 回答 1

2

服务栈 v4

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

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);
}

服务栈 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  },
    });
}

由 Mythz 在这里CustomActionHandler 提供

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; }
    }
}

希望有帮助。

于 2014-05-12T15:29:10.917 回答