0

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

Scott
  • 21,211
  • 8
  • 65
  • 72
CallumVass
  • 11,288
  • 26
  • 84
  • 154

1 Answers1

2

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.

Community
  • 1
  • 1
Scott
  • 21,211
  • 8
  • 65
  • 72
  • Ah, I'm using V3 still. I should have mentioned that – CallumVass May 12 '14 at 15:48
  • @BiffBaffBoff I added a v3 implementation. It's a bit longer because it requires that you implement the `CustomActionHandler` yourself (but that's just copy and paste as-is). But it is essentially the same as v4 (except v4 provides this class already). Hope that helps. – Scott May 12 '14 at 16:02
  • Thanks for that @Scott ! I also didn't know there was a V3 specific SS tag :) I will test it tomorrow as dont have the code here with me at the moment – CallumVass May 12 '14 at 17:09
  • @BiffBaffBoff Cool. Yeah it [was introduced last December](http://meta.stackexchange.com/questions/210765/significant-platform-change-should-a-new-tag-be-created-servicestack), but most people don't know about it. So I usually tag after v3 has been established. – Scott May 12 '14 at 17:12
  • Hi @Scott . This seems to return the html page as raw text and not as text/html ? – CallumVass May 13 '14 at 08:01
  • @BiffBaffBoff I know where I went wrong. Use `httpRes.WriteFile` instead of `TransmitFile`. I'll update the answer shortly. You can also set a content type header on the response too. – Scott May 13 '14 at 08:06
  • I tried WriteFile and get the same result. I also tried: `httpRes.ContentType = "text/html";` but I get an error: `Cannot access a disposed object.` – CallumVass May 13 '14 at 08:13
  • @BiffBaffBoff are you setting the header before the write, it won't work afterwards. I am not getting that issue. I will be able to double check shortly, but I am not in the office right now. – Scott May 13 '14 at 08:20
  • Perfect! I put the content type before WriteFile and it worked! Cheers! – CallumVass May 13 '14 at 08:23
  • @BiffBaffBoff nice one. :) – Scott May 13 '14 at 08:46