Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After much research, I have found the following that seems effective.</p> <h1>Configuration</h1> <p>In the <code>AppHost</code> constructor:</p> <pre><code> CatchAllHandlers.Add( (httpMethod, pathInfo, filePath) =&gt; Tims.Support.StaticFileHandler.Factory( Params.Instance.HttpDataDir, "/", pathInfo ) ); </code></pre> <h1>The factory</h1> <p>Checks for the existance of the file, and returns the appropriate handler, or returns null if it is not handling the file (because it does not exist). This is important so that other urls (such as <code>/metadata</code> continue to work.</p> <h1>Handler</h1> <p>The core method of the actual handler is very simple. By overriding <code>ProcessRequest</code> and returnign the bytes of the file with an appropriate resource type, the job is done. This version, for simplicity, does not include any date handling for caching purposes.</p> <h1>Sample Code</h1> <pre><code>public class StaticFileHandler : EndpointHandlerBase { protected static readonly Dictionary&lt;string, string&gt; ExtensionContentType; protected FileInfo fi; static StaticFileHandler() { ExtensionContentType = new Dictionary&lt;string, string&gt; (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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload