I try to build a web application with dotnet core web api,but i do not know how to set index.html as start page which can be done with dotnet framework web api easily. And i tri
Step 1
app.UseDefaultFiles();
app.UseStaticFiles();
Step 2
Create a folder called "wwwroot". put a file called index.html
Step 3 (optional)
If you are the using the auto generated template, you can remove make the launchUrl blank like this
"launchUrl": "",
Otherwise, you will have to manually keep going to the landing page every time during localhost running.
This is the correct way. But always use UseDefaultFiles()
before UseStaticFiles
Otherwise it won't work.
For reference: Core fundamentals of Static Files
For Asp.Net Core 2.2 right click on Project → Properties → Debug and next to Launch Browser checkbox set path to the startup page you want.
Project VS
If Index.html is in project root, it will be send by default.
In Properties/launchSettings.json
you can define the launchUrl
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "<your relative URL here>",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
For dotnet core web api 3.1, on launchSettings.json
file set "launchUrl": "swagger/index.html",
You can set any file in any folder under the wwwroot as defaut file by using options.DefaultFileNames.Add in startup.cs .
For example to use myfile.html in wwwroot/folder1/folder2/ myfile.html, you will add this in Startup.cs
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("folder1/folder2/ myfile.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
But some time it may not work. For example I created project File menu > New > Project , then selected .NET Core > ASP.NET Core Web Application and selected Web Api as project template. F5 always open page api/values, even though I added index.html in wwwroot folder and added following in startup.cs
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mypage.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
Then I opened project properties page and deleted the value in Debug/Launch browser box (which was set to api/values) Now setting of startup page is working and mypage.html is startup page. Note that this page should be in wwwroot folder as you have opted to use static files.