Posted on 04/02/2026 19:56:22
Hi Cecilia,
FYI: English is the preferred language on this forum :-)
That said, here's what is needed to get this working:
1. In a Class Library targeting .NET 8 and referencing DW 10, create a folder called Middleware.
2. Add to that folder the following class:
using Dynamicweb.Host.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace Your.Company.Middleware;
public sealed class RegisterCustomMiddleware : IPipeline
{
public void RegisterServices(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
{
}
public void RegisterApplicationComponents(IApplicationBuilder app)
{
app.UseStaticFilesForUnknownExtensions();
}
public void RunInitializers()
{
}
public int Rank => 200; // High number so we go after DW built-in stuff
}
This is the IPipeline implementation to register your own stuff. DW will find the class itself (because it implements IPipeline) and then calls methods like RegisterApplicationComponents where you can register your own middleware. In my case, I call UseStaticFilesForUnknownExtensions which is defined in another class you need to add to the middleware folder:
3. Add a class called StaticFileUnknownExtensionsMiddlewareExtensions with the following code:
using Microsoft.AspNetCore.Builder;
namespace Your.Company.Middleware;
internal static class StaticFileUnknownExtensionsMiddlewareExtensions
{
public static IApplicationBuilder UseStaticFilesForUnknownExtensions(this IApplicationBuilder app)
{
return app.UseWhen(
ctx =>
{
var path = ctx.Request.Path.Value;
if (string.IsNullOrEmpty(path))
return false;
var ext = Path.GetExtension(path);
return StaticFileUnknownExtensionsMiddleware.CustomMimeTypes.ContainsKey(ext);
},
branch => branch.UseMiddleware<StaticFileUnknownExtensionsMiddleware>()
);
}
}
This sets up the middleware onditionally. When the current request ends a with a file extension defined in StaticFileUnknownExtensionsMiddleware.CustomMimeTypes, it uses the middleware StaticFileUnknownExtensionsMiddleware which is the final class you need to add:
4. Add StaticFileUnknownExtensionsMiddleware with the following code:
using Dynamicweb.Core.Helpers;
using Microsoft.AspNetCore.Http;
using System.Web;
namespace Your.Company.Middleware;
internal sealed class StaticFileUnknownExtensionsMiddleware(RequestDelegate next)
{
internal static readonly Dictionary<string, string> CustomMimeTypes = new(StringComparer.InvariantCultureIgnoreCase)
{
// Custom MIME types can be added here
[".step"] = "application/step",
[".stp"] = "application/step"
};
public async Task Invoke(HttpContext ctx)
{
try
{
var path = FilePathHelper.GetAbsolutePath(HttpUtility.UrlDecode(ctx.Request.Path));
ctx.Response.Headers.ContentDisposition = $"attachment; filename=\"{Path.GetFileName(path)}\"";
await ctx.Response.SendFileAsync(path);
return;
}
catch (Exception ex)
{
// When there's a failure, ignore and let DW handle the request.
// Todo: Log error
}
await next(ctx);
}
}
This is the actual implementation that serves the physical file from disk using SendFileAsync. In CustomMimeTypes you register the custom extensions you like to suport. The code also sets the proper mime type which may help the browser to know what to do with it.
Hope this helps,
Imar