c# - ServiceStack: Serving Static files from a directory when present? -
i in process of converting stand-alone home grown web server use servicestack serving pages , resources.
i see question
serving static file servicestack
that easy serve single static file service stack.
in home grown implementation, after checking see if url matches particular handler (equivalent servicestack routes), default handler checks static file in httpdata directory serve matching url.
if file not exist, generates 404 error.
what best pattern use servicestack serve files file system if no other service matched? note using in standalone mode without iis.
these files can html, png, js, , small handful of other content types.
note: see servicestack.razor package may assist requiredments, cannot find documentation on it. put in separate question that.
edit: found reference in question
create route root path, '/', servicestack
indicating
register
iapphost.catchallhandlers
- gets called un-matched requests.
so far, have not found documentation or example on how to registration. note: running stand-alone, needs done in c#, not xml.
after research, have found following seems effective.
configuration
in apphost
constructor:
catchallhandlers.add( (httpmethod, pathinfo, filepath) => tims.support.staticfilehandler.factory( params.instance.httpdatadir, "/", pathinfo ) );
the factory
checks existance of file, , returns appropriate handler, or returns null if not handling file (because not exist). important other urls (such /metadata
continue work.
handler
the core method of actual handler simple. overriding processrequest
, returnign bytes of file appropriate resource type, job done. version, simplicity, not include date handling caching purposes.
sample code
public class staticfilehandler : endpointhandlerbase { protected static readonly dictionary<string, string> extensioncontenttype; protected fileinfo fi; static staticfilehandler() { extensioncontenttype = new dictionary<string, string> (stringcomparer.invariantcultureignorecase) { { ".text", "text/plain" }, { ".js", "text/javascript" }, { ".css", "text/css" }, { ".html", "text/html" }, { ".htm", "text/html" }, { ".png", "image/png" }, { ".ico", "image/x-icon" }, { ".gif", "image/gif" }, { ".bmp", "image/bmp" }, { ".jpg", "image/jpeg" } }; } public string basedirectory { protected set; get; } public string prefix { protected set; get; } public staticfilehandler(string basedirectory, string prefix) { basedirectory = basedirectory; prefix = prefix; } private staticfilehandler(fileinfo fi) { this.fi = fi; } public static staticfilehandler factory(string basedirectory, string prefix, string pathinfo) { if (!pathinfo.startswith(prefix, stringcomparison.invariantcultureignorecase)) { return null; } var fn = basedirectory + "/" + pathinfo.after(prefix.length); var fi = new system.io.fileinfo(fn); if (!fi.exists) { return null; } return new staticfilehandler(fi); } public override void processrequest(ihttprequest httpreq, ihttpresponse httpres, string operationname) { using (var source = new system.io.filestream(fi.fullname, system.io.filemode.open)) { var bytes = source.readallbytes(); httpres.outputstream.write(bytes, 0, bytes.length); } // timestamp = fi.lastwritetime; httpres.addheader("date", datetime.now.tostring("r")); httpres.addheader("content-type", extensioncontenttype.safeget(fi.extension) ?? "text/plain"); } public override object createrequest(ihttprequest request, string operationname) { return null; } public override object getresponse(ihttprequest httpreq, ihttpresponse httpres, object request) { return null; } }
Comments
Post a Comment