How to set start page in dotnet core web api?

后端 未结 9 581
广开言路
广开言路 2021-01-31 07:56

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

相关标签:
9条回答
  • 2021-01-31 07:58

    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

    0 讨论(0)
  • 2021-01-31 08:03

    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

    0 讨论(0)
  • 2021-01-31 08:05

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

    0 讨论(0)
  • 2021-01-31 08:10

    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"
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-31 08:13

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

    0 讨论(0)
  • 2021-01-31 08:14

    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.

    0 讨论(0)
提交回复
热议问题