Kasrak
Kasrak

Reputation: 1561

Blazor - Static Html file routing

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"

Upvotes: 4

Views: 2057

Answers (1)

Amal K
Amal K

Reputation: 4919

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().

Upvotes: 5

Related Questions