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.
-
2Where is the index.html? In wwwroot folder or project root directory?adem caglin– adem caglin2016-11-17 05:18:25 +00:00Commented Nov 17, 2016 at 5:18
-
1@ademcaglin wwwroot.i solved this problem by typing the right url in the browser.when i start the application,the browser will start with a url like ip:portnumber/api/values and i ignored it.just app.UseDefaultFiles();app.UseStaticFiles(); is ok.HongyanShen– HongyanShen2016-11-17 05:59:33 +00:00Commented Nov 17, 2016 at 5:59
10 Answers
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"
}
}
}
4 Comments
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
2 Comments
If you are using a static file as the default page, the following code can help you.
app.UseDefaultFiles(new DefaultFilesOptions { DefaultFileNames = new
List<string> { "index.html" } });
If you are using the MVC view, just add the routing role.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
5 Comments
For Asp.Net Core 2.0/2.1/2.2 just right click on Project → Properties → Debug and next to Launch Browser checkbox set path to the startup page you want.
1 Comment
Your index.html file must be in the wwwroot folder
wwwroot / index.html
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files
5 Comments
wwwroot folder. This answer is relevant to the question.app.UseDefaultFiles(); and app.UseStaticFiles(); as in @sebulbamalastare answer and index.html file in the wwwroot folder.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.
3 Comments
"launchUrl": "weatherforecast" which I had to remove from launchSettings.json (this is ASP.NET Core 3.1). It was a leftover from the project template that is created in this version. Static page showed up in development, but not in production. I just removed all instances of launchUrl and it worked.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.
Comments
net6,webapi
This is my implementation code, I hope it can help you!
Step 1
file:Program.cs
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Step 2
newfile:wwwroot/index.html
Hello,World!
Step 3
newfile:Controllers/HomeController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace zgcwkj.Controllers
{
/// <summary>
/// Home
/// </summary>
[AllowAnonymous]
public class HomeController : Controller
{
/// <summary>
/// WebHost
/// </summary>
private IWebHostEnvironment _IWebHost { get; }
/// <summary>
/// Home
/// </summary>
public HomeController(IWebHostEnvironment iWebHost)
{
this._IWebHost = iWebHost;
}
/// <summary>
/// Index
/// </summary>
public IActionResult Index()
{
var rootPath = _IWebHost.WebRootPath;
var rootIndex = $"{rootPath}/index.html";
if (!System.IO.File.Exists(rootIndex))
{
return View("No File");
}
var fileResult = PhysicalFile(rootIndex, "text/html; charset=UTF-8");
return fileResult;
}
}
}
Step 4
run your webapi