This is required to access IWebHostEnvironment
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
NOTE: This will allow to embed all files inside WebRoot as well as subfolders
<!-- Add this ItemGroup to embed your static files -->
<ItemGroup>
<EmbeddedResource Include="WebRoot\**\*.*" />
</ItemGroup>
[ApiExplorerSettings(IgnoreApi = true)]
only blocks the controller's visibility to Swagger. The controller will still be active
using Microsoft.AspNetCore.Mvc;
namespace Twileloop.WebEmbed
{
[ApiController]
[Route("[controller]")]
[ApiExplorerSettings(IgnoreApi = true)]
public class PerfomanceController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet(Name = "Perf")]
public IActionResult Get()
{
return Ok(Enumerable.Range(1, 5).Select(index => new
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray());
}
}
}
IMPORTANT: Embedded paths hould begin with your namespace name like "Twileloop.WebEmbed.WebRoot"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Reflection;
namespace Twileloop.WebEmbed
{
public class PerformanceMiddleware
{
private readonly RequestDelegate _next;
private readonly StaticFileMiddleware _staticFileMiddleware;
public PerformanceMiddleware(
RequestDelegate next,
IWebHostEnvironment hostingEnv,
ILoggerFactory loggerFactory,
PerformanceMiddlewareOptions options)
{
_next = next;
_staticFileMiddleware = CreateStaticFileMiddleware(hostingEnv, loggerFactory, options);
}
public async Task Invoke(HttpContext httpContext)
{
// Check if the URL starts with the specified base URL
if (httpContext.Request.Path.StartsWithSegments("/performance"))
{
await _staticFileMiddleware.Invoke(httpContext);
return;
}
// Continue to the next middleware if the URL doesn't match
await _next(httpContext);
}
private StaticFileMiddleware CreateStaticFileMiddleware(
IWebHostEnvironment hostingEnv,
ILoggerFactory loggerFactory,
PerformanceMiddlewareOptions options)
{
var assembly = typeof(PerformanceMiddleware).GetTypeInfo().Assembly;
var staticFileOptions = new StaticFileOptions
{
RequestPath = "/performance",
FileProvider = new EmbeddedFileProvider(assembly, options.FolderName),
};
// Use the StaticFileMiddleware constructor directly
return new StaticFileMiddleware(
async context => await _next(context),
hostingEnv,
Options.Create(staticFileOptions),
loggerFactory);
}
}
public class PerformanceMiddlewareOptions
{
public string FolderName { get; set; } = "Twileloop.WebEmbed.WebRoot";
}
public static class PerformanceMiddlewareExtensions
{
public static IApplicationBuilder UsePerformanceMiddleware(
this IApplicationBuilder builder,
PerformanceMiddlewareOptions options)
{
return builder.UseMiddleware<PerformanceMiddleware>(options);
}
}
}
Register Controllers from our new NuGet package in main app
builder.Services.AddControllers()
.PartManager.ApplicationParts.Add(new AssemblyPart(typeof(PerfomanceController).Assembly));
app.UsePerformanceMiddleware(new PerformanceMiddlewareOptions
{
FolderName = "Twileloop.WebEmbed.WebRoot"
});