86

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.

2
  • 2
    Where is the index.html? In wwwroot folder or project root directory? Commented 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. Commented Nov 17, 2016 at 5:59

10 Answers 10

127

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"
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This worked, but not in the way I need it to: it actually redirects you to a different URL rather than making "mysite.com/page1" == "mysite.com". So when you browse "mysite.com", you are redirected to "mysite.com/page1". What I want is to display page1 to the user but still show "mysite.com" on their browser.
Hey @demonicdaron, Sounds like you need Attribute Routing. See here: learn.microsoft.com/en-us/aspnet/core/mvc/controllers/…
I Suggest adding: launchSettings.json can be found in the Properties folder.
nice and simple answer! +1
70

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

These 2 lines combined with having the index.html file the wwwroot folder worked like a charm.
Will this work the same for Razor pages? I've got it to work with a .html page, but when I try with a .cshtml page, it doesn't load it.
62

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

This needs to be done in Startup.cs => Configure() method.
I tried using MapRoute as suggested above, but it had no effect. I tried @Floris answer and worked, but not in the way I need it to: it actually redirects you to a different URL rather than making "mysite.com/page1" == "mysite.com", which is what I would expect MapRoute to do.
@demonicdaron, if you want a static file as start page, you need put file into wwwroot directory, for example, if I want set index.html as start page, just put index.html into wwwroot and add code as above mentioned. If you want a set a controller as start page, just modify routing template.
I have several subfolders under Pages/ and want the site to default to one of those folders' index page, without moving that index page out of its folder. Is this possible at all? Or must I move the default page out of its folder and into the wwwroot folder at all costs?
seems you are using asp.net core 2.1 web application, and use new style of cshtml view, just add folder in Pages, and rename cshtml file as Index in the forder. by the way app.UseDefaultFiles seems not work in this style mvc project.
21

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

This was set to the default API on project creation in Visual Studio, which meant none of the other solutions mentioned worked but this did, thanks!
15

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

This question has an accepted answer since a year ago. Are you sure that your answer is relevant?
This is very helpful. I am using ASP.NET Core 3.1. I tried accepted answer and all above high voted answers, none worked. Those answers only work if the static file is in wwwroot folder. This answer is relevant to the question.
Basically what we need is app.UseDefaultFiles(); and app.UseStaticFiles(); as in @sebulbamalastare answer and index.html file in the wwwroot folder.
My asp.net core 3.1 flutter index.html not accessible. I added DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("Views/index.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); app.UseRouting(); and cleared launch browser url - error no localhost page can't be found
I had to copy all my flutter web build files to wwwroot and then the javascript worked.
7

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

Clearing "api/values" from Project>Properties>Debug>Launch Browser did the trick for me.
I have tried adding var defaultFileOptions = new DefaultFilesOptions(); defaultFileOptions.DefaultFileNames.Add("../Pages/SomeFolder/Index.cshtml"); app.UseDefaultFiles(defaultFileOptions); but that did not work. I have several subfolders under Pages/ and want the site to default to one of those folders' index page, without moving that index page out of its folder.
"api/values" was not shown in Launch Browser box for me, but there was a line "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.
3

For dotnet core web api 3.1, on launchSettings.json file set "launchUrl": "swagger/index.html",

Comments

2

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

Comments

1

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

If Index.html is in project root, it will be send by default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.