当我对 运行 我的站点使用 windows 服务时,我无法访问 wwwroot 中的 index.html

I can't access index.html in wwwroot when I use windows service to run my site

当我使用 windows 服务 到 运行 我的站点时,我无法访问我在 wwwroot 中的静态文件,但是当我使用IIS Express 或 IIS。 我使用 Asp.Net Core 2.2.

构建项目

controller里面的动作没问题,只是静态文件无法访问

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        var df = new DefaultFilesOptions();
        df.DefaultFileNames.Add("Index.html");
        app.UseDefaultFiles(df);
        app.UseStaticFiles();
        app.UseMvc();
    }
}


public class Program
{
    public static void Main(string[] args)
    {
        var isService = !(Debugger.IsAttached || args.Contains("--console"));
        if (isService)
        {
            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe);
            Directory.SetCurrentDirectory(pathToContentRoot);
            var host = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray()).Build();
            host.RunAsService();
        }
        else
        {
            WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().Build().Run();
        }
    }
    static int Port = 9099;
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseKestrel(options => { options.Listen(IPAddress.Any, Port); })
        .UseStartup<Startup>();
}

这是因为您在 运行 应用程序作为 windows 服务时设置目录。

而不是这样做

var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);

调整您的 Webhost-Build 定义

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseKestrel(options => { options.Listen(IPAddress.Any, Port); })
        .UseStartup<Startup>()
        .UseContentRoot(AppContext.BaseDirectory); // add this line

然后在Startupclass添加静态文件选项

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    var df = new DefaultFilesOptions();
    // these options are not necessary index.html is added by default
    df.DefaultFileNames.Add("Index.html");
    app.UseDefaultFiles(df);
    app.UseStaticFiles(new StaticFileOptions
      {
         FileProvider = env.WebRootFileProvider
      });
    app.UseMvc();
}

还要确保您的 index.html 始终被复制到输出目录。 要么将其添加到您的 csproj 文件

<Content Update="wwwroot\index.html">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

或在 visual-studio 中右键单击它 > 属性 > 复制到输出目录 > 始终复制