Last active
February 2, 2017 04:56
-
-
Save cwharris/8ac4190c90d8565e066d600fbe62910f to your computer and use it in GitHub Desktop.
AspNetCore SinglePageMiddleware.cs
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
using System; | |
using System.Threading.Tasks; | |
using System.Threading; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.StaticFiles; | |
using Microsoft.Extensions.FileProviders; | |
using Microsoft.Extensions.Logging; | |
namespace ChristopherHarris | |
{ | |
public class SinglePageMiddleware | |
{ | |
private ILogger<SinglePageMiddleware> _logger; | |
private IFileProvider _fileProvider; | |
private string _filename; | |
private IContentTypeProvider _contentTypeProvider; | |
public SinglePageMiddleware( | |
RequestDelegate next, | |
IHostingEnvironment env, | |
ILogger<SinglePageMiddleware> logger, | |
string filename) | |
{ | |
_logger = logger; | |
_filename = filename; | |
_fileProvider = env.WebRootFileProvider; | |
_contentTypeProvider = new FileExtensionContentTypeProvider(); | |
} | |
public async Task Invoke(HttpContext context) | |
{ | |
var request = context.Request; | |
var response = context.Response; | |
var fileInfo = _fileProvider.GetFileInfo(_filename); | |
if (!fileInfo.Exists) | |
{ | |
_logger?.LogError($"Unable to find file: {_filename}"); | |
response.StatusCode = StatusCodes.Status404NotFound; | |
return; | |
} | |
if (!request.Method.Equals(HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) | |
{ | |
_logger?.LogInformation($"Request made via incompatible method: {request.Method}"); | |
response.StatusCode = StatusCodes.Status400BadRequest; | |
return; | |
} | |
_logger?.LogTrace($"Delivering single static file: {_filename}"); | |
response.StatusCode = StatusCodes.Status200OK; | |
response.ContentLength = fileInfo.Length; | |
response.ContentType = GetContentType(_filename); | |
await response.SendFileAsync( | |
fileInfo.PhysicalPath, | |
0, | |
fileInfo.Length, | |
CancellationToken.None); | |
} | |
private string GetContentType(string filename) | |
{ | |
string contentType = null; | |
_contentTypeProvider.TryGetContentType(filename, out contentType); | |
return contentType; | |
} | |
} | |
} |
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
using Microsoft.AspNetCore.Builder; | |
namespace ChristopherHarris | |
{ | |
public static class SinglePageMiddlewareExtensions | |
{ | |
public static void UseSinglePage(this IApplicationBuilder app, string filename) | |
{ | |
app.UseMiddleware<SinglePageMiddleware>(filename); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment