使用Windows服务运行网站时,我无法在wwwroot中访问index.html

时间:2019-01-23 07:25:00

标签: asp.net-core asp.net-web-api2 asp.net-core-webapi

使用 windows服务来运行站点时,我无法访问wwwroot中的静态文件,但是当使用 IIS Express或IIS 时,它可以工作。 我使用Asp.Net Core 2.2构建项目。

控制器中的动作正常,仅静态文件无法访问。

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>();
}

1 个答案:

答案 0 :(得分:0)

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

而不是这样做

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

调整您的Webhost构建定义

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

然后在Startup类中添加静态文件选项

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中右键单击它>属性>复制到输出目录>始终复制