Created
September 7, 2019 16:21
-
-
Save adamdriscoll/f3081261d069d36f9ccabe9276b07d7b to your computer and use it in GitHub Desktop.
Arbitrary Asset Loading in Universal Dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.UseStatusCodePages(async context => { | |
if (context.HttpContext.Response.StatusCode == 404 && !context.HttpContext.Request.Path.StartsWithSegments("/api")) | |
{ | |
var fileName = context.HttpContext.Request.Path.ToString().Split('/').Last(); | |
var asset = AssetService.Instance.FindAsset(fileName); | |
if (asset != null) | |
{ | |
var response = context.HttpContext.Response; | |
response.StatusCode = 200; | |
var file = File.ReadAllBytes(asset); | |
await response.Body.WriteAsync(file, 0, file.Length); | |
} | |
else | |
{ | |
var response = context.HttpContext.Response; | |
response.StatusCode = 200; | |
var filePath = env.ContentRootPath + "/index.html"; | |
response.ContentType = "text/html; charset=utf-8"; | |
var file = File.ReadAllText(filePath); | |
await response.WriteAsync(file); | |
} | |
} | |
else | |
{ | |
await context.Next(context.HttpContext); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment