In my web application I have a controller with a fallback-route so that every request goes to this controller and additionally I use the PhysicalFileProvider
to serve static files from a specific directory.
My fallback-controller:
<code>[ApiController]
[AllowAnonymous]
[Route("")]
public class PageRenderingController : ControllerBase
{
[HttpGet("{*url:nonfile}")]
public async Task<ActionResult> Get([FromRoute]string? url = null)
{
// return result
}
}
</code>
<code>[ApiController]
[AllowAnonymous]
[Route("")]
public class PageRenderingController : ControllerBase
{
[HttpGet("{*url:nonfile}")]
public async Task<ActionResult> Get([FromRoute]string? url = null)
{
// return result
}
}
</code>
[ApiController]
[AllowAnonymous]
[Route("")]
public class PageRenderingController : ControllerBase
{
[HttpGet("{*url:nonfile}")]
public async Task<ActionResult> Get([FromRoute]string? url = null)
{
// return result
}
}
My Program.cs
:
<code>public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// register services...
var app = builder.Build();
SetupFileServer(app);
app.MapControllers();
app.Run();
}
private static void SetupFileServer(WebApplication app)
{
var staticAdminFileServerOptions = new FileServerOptions
{
EnableDefaultFiles = true,
FileProvider = new PhysicalFileProvider(
Path.Combine(app.Environment.ContentRootPath, "_admin")),
RequestPath = "/admin",
EnableDirectoryBrowsing = false,
};
app.UseFileServer(staticAdminFileServerOptions);
}
</code>
<code>public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// register services...
var app = builder.Build();
SetupFileServer(app);
app.MapControllers();
app.Run();
}
private static void SetupFileServer(WebApplication app)
{
var staticAdminFileServerOptions = new FileServerOptions
{
EnableDefaultFiles = true,
FileProvider = new PhysicalFileProvider(
Path.Combine(app.Environment.ContentRootPath, "_admin")),
RequestPath = "/admin",
EnableDirectoryBrowsing = false,
};
app.UseFileServer(staticAdminFileServerOptions);
}
</code>
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// register services...
var app = builder.Build();
SetupFileServer(app);
app.MapControllers();
app.Run();
}
private static void SetupFileServer(WebApplication app)
{
var staticAdminFileServerOptions = new FileServerOptions
{
EnableDefaultFiles = true,
FileProvider = new PhysicalFileProvider(
Path.Combine(app.Environment.ContentRootPath, "_admin")),
RequestPath = "/admin",
EnableDirectoryBrowsing = false,
};
app.UseFileServer(staticAdminFileServerOptions);
}
So my problem now is that even I registered the FileServer before the controllers, a request to http://host/admin/
goes to the controller instead of the FileServer (and serving the index.html file). Any ideas how to solve that?