我正在尝试在Asp.Net Core中使用UseFileServer,但无法正常工作

时间:2019-06-16 14:41:54

标签: asp.net asp.net-core static-files

我已经安装了Microsoft.AspNetCore.StaticFiles(NuGet程序包),下面是相关的启动代码。我在根目录和名为static的文件夹中都有一个index.html文件。

总而言之,这似乎很简单……不确定我为什么遇到麻烦。

在本地主机上的调试模式下...我得到:

https://localhost:44331(404)

https://localhost:44331/index.html(404)

https://localhost:44331/static(404)

https://localhost:44331/static/index.html(200)

https://localhost:44331/api/values(200)

当我发布到azure Web应用程序服务器时,以上URL均无效,除了:

https://myserver.com/api/values(200)

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }



    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();

        app.UseFileServer();
    }

enter image description here

根据答案进行更新(有效)

enter image description here

1 个答案:

答案 0 :(得分:0)

为了正确提供服务,您的文件应位于wwwroot文件夹中。

如果您将wwwroot放在index.html中,则可以通过htts://localhost:44331/index.html访问它。