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

Wei

当我使用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>();
}
阿尔萨米

这是因为您在将应用程序作为 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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法访问我服务器中的网站

当我当前在子目录中时如何返回根目录中的 index.html

为什么我无法访问托管在 Windows 服务中的 wcf?

当我从 index.html 转到 index.html/#contact 时,为什么我的 CSS 消失了?

背景图片url()可在实时服务器上使用,但是当我在浏览器中打开index.html时,它不能使用吗?

部署项目时无法访问“ wwwroot”中的文件

当我没有index.html时出现

我无法访问远程WAMP服务器中的Wordpress网站,因为我的IP地址已更改

当我运行我的 html 代码时,它在 chrome (html) 中显示为空白

从wwwroot提供的index.html文件中的脚本/样式表链接返回404

当我只能访问CSS(我无法更改HTML)时,如何更改引导程序中的列宽?

当我再也无法访问Windows 10时,如何删除Microsoft(工作)帐户?

当我在 Expo (iOS) 中使用 Print API 打印 html 时,html 中的分页符无法按预期工作

当我使用index.php / User / login可以正常工作时,但是当我使用User.login时即使在codeigniter中删除了index.php也无法正常工作

当我运行使用 Windows api 编写的代码时,它会在打开后立即关闭(在 Visual Studio 中)

我无法使用Angularjs在index.html中包含另一个html页面

我在 Windows PowerShell 中运行命令时出现“访问被拒绝”

当我使用“ ng-list”时,我无法在javascript中访问变量模型

为什么我无法访问Windows的Linux子系统中的Windows用户文件夹

当我使用 .innerHTML 函数查看 html 中的变量时未捕获的类型错误

当我使用php调用数据时,如何在textarea标签中隐藏html代码?

使用API中的HTML而不是index.html中的静态HTML来水化我的React应用

即使我有权限也无法访问路径wwwroot

当我从Python中的Google搜索查询中提取链接时,我无法返回HTML链接

当我单击HTML网页中的任意位置时,我使用Jquery获得了该标签的ID

当我使用HTML5`contenteditable`时,`button`元素中的空格键“无效”。我如何才能正常工作?

当我尝试使用php从数据库中回显数据时,我的html页面坏了

我无法从Parse Dashboard上传PFFile(图像),当我的解析服务器使用https尝试在浏览器中访问它时,我得到404

当我在浏览器中输入我的 vps ip 时显示“无法访问此站点”