Html5 pushstate Urls on ServiceStack

We are currently using the default.cshtml view in the root of ServiceStack to serve a single-page AngularJS application.

What I would like to do is enable html5 pushstate support (so that hash in the url), but the only examples I have found so far are related to MVC dependency with wildcard pattern and pushing the ServiceStack infrastructure to the / api routine .

We can’t take the MVC dependency, so I think we need to accept: text / html requests that we need to accept any URL and serve our root application. I would be happy to remove the default HtmlFormat extension or override it (we could still use its JsonReport content type that we need).

How can I best approach this?

+4
source share
2 answers

The Wiki Actions Procedure page shows the number of different hooks you can use to enter your own customized behavior, as well as the order in which they are executed.

Greeting requests using RawHttpHandlers

You can completely bypass ServiceStack by adding Config.RawHttpHandlers to return IHttpHandler to the requests you want to welcome, for example, since the built-in mini-profiler welcomes all requests for files that are launched with ssr- and returns a physical file:

 config.RawHttpHandlers.Add((IHttpRequest request) => { var file = GetFileNameWithoutExtension(request.PathInfo); return file != null && file.StartsWith("ssr-") ? new MiniProfilerHandler() : null; } 

Providing a failover route for inconsistent routes

If you want to provide a default handler for an inconsistent route, you can register CatchAllHandlers in AppHost.Configure() or in a plugin with:

 appHost.CatchAllHandlers.Add((string method, string pathInfo, string filepath) => { return ShouldProvideDefaultPage(pathInfo) ? new RazorHandler("/defaultpage.cshtml") : null; }); 

Using a wildcard to accept any URL in a service

You can create a dummy service and simply return the same view, for example:

 [Route("/app/{PathInfo*}")] public class App { public string PathInfo { get; set; } } public class MyService : Service { public object Any(App request) { return request; } } 

Using a wild card, this service will return the view, for example. /View/App.cshtml on any route starting with /app , for example:

  • /attachment
  • / app / hello
  • / app / hello / my / name / is this? Foo = bar

Partial Page Support

Since a partial reboot is related to pushstate, I also mention the built-in support of ServiceStack for a partial reboot.

ServiceStack Docs is an example of a demo that uses pushstate in browsers that support it, otherwise it reverts to using full-page reloads with browsers that don't.

You can request a partial page with parameter ?format=text.bare , for example.

Although it uses Markdown Razor . In the latest ServiceStack.Razor service, you can access the partial page only with ?format=bare

+5
source

Extension of my comment. This is what I ended up trying to host the application in / app, and also support the virtual file system.

 host.CatchAllHandlers.Add((string method, string pathInfo, string filepath) => { if (!Regex.IsMatch(pathInfo, "^/app([/?]|$)")) return null; // Serve valid requests as is var vFile = HostContext.ResolveVirtualFile(pathInfo, null); if (vFile != null) return null; var vDir = HostContext.ResolveVirtualDirectory(pathInfo, null); if (vDir != null && vDir.GetDefaultDocument() != null) return null; // Fallback to default document var vDef = HostContext.ResolveVirtualDirectory("/app/", null).GetDefaultDocument(); return new CustomResponseHandler((req, res) => new HttpResult(vDef.OpenRead(), MimeTypes.GetMimeType(vDef.Name))); }); 
+1
source

All Articles