问题
I have a self-hosted application which has an index.html
file at its root. When I run the application and go to localhost:8090
(app is hosted on this port) the URL looks like: http://localhost:8090/index.html
. Is there anyway I can make the URL to just be: http://localhost:8090
when on the index.html page?
Note
I'm using V3 of ServiceStack
回答1:
ServiceStack v4
In ServiceStack v4 I use a raw http handler to intercept the root. In your AppHost Configure
method:
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
In ServiceStack v3 you can do a similar thing, but you will have to include the CustomActionHandler
class yourself. So in your configure method:
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 },
});
}
The CustomActionHandler
as provided 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; }
}
}
Hope that helps.
来源:https://stackoverflow.com/questions/23609330/servicestack-url-re-writing-with-self-hosted-application