Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor - Static Html file routing

Tags:

c#

.net-5

blazor

Here I have a static HTML file (index.html) placed in a folder named Test inside wwwroot.

How can I configure the app to make it accessible at /Test address? Currently seems I need to specify the whole address: "test/index.html"

like image 637
Kasrak Avatar asked Oct 28 '25 07:10

Kasrak


1 Answers

To automatically look for the following files, when you route to a directory in wwwroot:

  • default.htm
  • default.html
  • index.htm
  • index.html

Just before UseStaticFiles() in the Configure method in Startup.cs, add a call to UseDefaultFiles():

app.UseDefaultFiles();
app.UseStaticFiles();

If you want file names other than the four mentioned above, you can create an instance of DefaultFilesOptions and add the file names you want:

var options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mycustomfilename.html");

app.UseDefaultFiles(options);
app.UseStaticFiles();

For more info, see serving default documents.

Also, if you want to allow directory browsing, you can replace both the above calls with:

app.UseFileServer(enableDirectoryBrowsing: true);

The above combines UseDefaultFiles(), UseStaticFiles() and UseDirectoryBrowser().

like image 72
Amal K Avatar answered Oct 31 '25 01:10

Amal K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!