How to set start page in dotnet core web api?

后端 未结 9 586
广开言路
广开言路 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 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.

提交回复
热议问题