ServiceStack Self Hosting Redirect any 404 for SPA (Url Rewrite)

361 views Asked by At

I'm using self hosting servicestack for a single page application with mvc and razor. On the same apphost i serve some api. The problem is i need some a 404 redirect to the main path "/" to get html5 mode running in AngularJS. I've tried:

DefaultRedirectPath = "/"
this.GlobalHtmlErrorHttpHandler = new RazorHandler("/");

How can i get any 404 request to the main page?

1

There are 1 answers

3
mythz On BEST ANSWER

You can register a Fallback Route with a wildcard for this which will let you handle unknown server routes in your Service in order to return the default page so routing can be handled in the client app, e.g:

[FallbackRoute("/{PathInfo*}")]
public class FallbackForClientRoutes
{
    public string PathInfo { get; set; }
}

public class MyServices : Service
{
    public object Any(FallbackForClientRoutes request)
    {
        return new HttpResult {
            View = "/default.cshtml"
        };
    }
}

This is also the strategy used by the techstacks.io AngularJS Single Page App which also supports server-generated pages with techstacks.io/?html=server.