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 tried to use app.UseDefaultFiles();app.UseStaticFiles();
to solve this problem, however, it did not work.
From the new item window, select the HTML template, provide the name as “index. html” and then click on the Add button as shown in the below image. Once you add the HTML Page within the wwwroot folder, your project folder structure should be shown like below. Now, open the index.
The Startup class ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.
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" } } }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With