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; }
}
}
希望对您有所帮助。