url routing - How can I get AngularJS working with the ServiceStack FallbackRoute attribute to support HTML5 pushstate Urls? -
i building client/server solution, using angularjs single page app client component , self-host servicestack restful api server component. single visual studio console application project holds html , javascript files angularjs component, along c# classes bootstrapping servicestack apphost (i have devolved interface , service responsibilities separate visual studio projects).
i have set html , javascript files have 'build action' of 'none' , 'copy output directory' of 'copy if newer'.
everything working long prepared put having '#' in site urls. eliminate using html5 pushstate urls.
effectively means need persuade servicestack serve default single page app html shell page whenever non-existent route requested. there fallbackroute attribute available in servicestack appears have been added purpose.
however, unsure how use it. have found people asking similar questions here, here , here. answers given before new fallbackroute attribute arrived.
essentially, looking simple, yet complete example of how use fallbackroute attribute ensure requests non-existent routes redirected single static html page.
the razorrockstars.web has implementation. i'll modify use wildcard path , default view:
[fallbackroute("/{path*}")] public class fallback { public string path { get; set; } public string pathinfo { get; set; } } public class rockstarsservice : service { [defaultview("index")] public object any(fallback request) { request.pathinfo = base.request.pathinfo; return request; } // ... }
since service requires view page (details here) rather content page.
in rockstars example, can't determine view rendered fallbackresponse, setting view explicitly should need.
the [defaultview("index")]
attribute added any
method maps response views/index.cshtml file. index.cshtml file can empty template declaration, , complete markup single page app can in template file (i.e. _layout.cshtml)
without razor
read html string , return it, while setting content type "text/html" attribute, see wiki docs on service return types
public class rockstarsservice : service { static string readcontents; [addheader(contenttype = "text/html")] public string any(fallback request) { // check timestamp changes production use if (readcontents == '') { using (streamreader streamreader = new streamreader(pathfromconfigfile, encoding.utf8)) { readcontents = streamreader.readtoend(); } } return readcontents; } // ... }
Comments
Post a Comment