Asp.Net Core 2.0-2.2 Kestrel 不提供静态内容 [英] Asp.Net Core 2.0-2.2 Kestrel not serving static content

查看:27
本文介绍了Asp.Net Core 2.0-2.2 Kestrel 不提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 IIS express 运行 Asp.Net Core 2.0(或 2.2)应用程序时,静态文件(css、js)按预期提供.但是,当通过dotnet publish -o [targetDirectory]"和 dotnet [website.dll] 使用命令行/Kestrel 时,Kestrel 不提供任何静态内容.使用浏览器 F12,我看到 Kestrel 返回 404 错误.仔细一看,当直接在浏览器中输入文件路径(localhost:5000/css/cssfile.css)时,文件没有显示,但浏览器似乎重定向到localhost:5000/cssfile.css" 但仍然返回 404 错误(注意缺少/css/目录).

我通过 Visual Studio 2017 创建了这个项目,并为新的 MVC Core 2.0 应用程序(安装了 SDK)选择了默认值.

我已按照此处的步骤启用program.cs 和 startup.cs 文件中的静态文件.这些实现app.UseStaticFiles();"和.UseContentRoot(Directory.GetCurrentDirectory())".通过谷歌找到的文章似乎都没有帮助.我已验证 dotnet 已将静态内容复制到目标目录.

我错过了什么?谢谢

//Program.cs公共静态 IWebHost BuildWebHost(string[] args) =>虚拟主机.CreateDefaultBuilder(args).UseStartup<启动>().建造();//启动.cs公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment 环境){app.UseExceptionHandler("/Error/HandleError");app.UseStaticFiles();app.UseMvc(routes =>{route.MapRoute( name: "default", template: "{controller=User}/{action=Index}/{id?}");});}

解决方案

我无法按照这些步骤使用新的 ASP.NET Core 2.0 项目重现您的错误;

  1. md 静态测试
  2. cd 静态测试
  3. dotnet 新网站
  4. 在 wwwroot 中添加文件夹 css.
  5. wwwroot/css中添加文件site.css.
  6. Startup.Configure() 方法的开头插入 app.UseStaticFiles();.
  7. dotnet publish -o pubweb
  8. cd pubweb
  9. dotnet .static-test.dll
  10. 使用浏览器访问 http://localhost:5000/css/site.css.

dotnet.exe 在我的终端中呈现以下输出:

托管环境:生产内容根路径:C:srcstatic-testpubweb现在监听:http://localhost:5000应用程序启动.按 Ctrl+C 关闭.信息:Microsoft.AspNetCore.Hosting.Internal.WebHost[1]请求开始 HTTP/1.1 GET http://localhost:5000/css/site.css信息:Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]发送文件.请求路径:'/css/site.css'.物理路径:'C:srcstatic-testpubwebwwwrootcsssite.css'

如您所见,它将成功正确地为子文件夹中的 css 文件提供服务.请尝试上述步骤并将代码和输出与您失败的项目进行比较.如果仍然失败,请在 RequestPhysical 路径上附加调试信息,来自上面的 Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.>

When running a Asp.Net Core 2.0 (or 2.2) app using IIS express, static files (css, js) are served as expected. However when using command line/Kestrel via "dotnet publish -o [targetDirectory]" and dotnet [website.dll], Kestrel does not serve up any static content. Utilizing browser F12, I see Kestrel returns a 404 error. Looking closer, when entering the file path directly in the browser (localhost:5000/css/cssfile.css) the file is not displayed but the browser appears to redirect to "localhost:5000/cssfile.css" but still returns a 404 error (note the missing /css/ directory).

I created this project via Visual Studio 2017, and chose the defaults for a new MVC Core 2.0 application (SDK was installed).

I have followed the steps here to enable static files in the program.cs and startup.cs files. These implement "app.UseStaticFiles();" and ".UseContentRoot(Directory.GetCurrentDirectory())". None of the articles found via google seem to help. I have verified dotnet copied the static content to the target directory.

What am I missing? Thanks

// Program.cs
public static IWebHost BuildWebHost(string[] args) => WebHost
   .CreateDefaultBuilder(args)
   .UseStartup<Startup>()
   .Build();

// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseExceptionHandler("/Error/HandleError");
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute( name: "default", template: "{controller=User}/{action=Index}/{id?}");
    });
}

解决方案

I am unable to reproduce your error using a fresh ASP.NET Core 2.0 project following these steps;

  1. md static-test
  2. cd static-test
  3. dotnet new web
  4. Add folder css in wwwroot.
  5. Add file site.css in wwwroot/css.
  6. Insert app.UseStaticFiles(); at the start of Startup.Configure() method.
  7. dotnet publish -o pubweb
  8. cd pubweb
  9. dotnet .static-test.dll
  10. Access http://localhost:5000/css/site.css using browser.

dotnet.exe renders the following output in my terminal:

Hosting environment: Production
Content root path: C:srcstatic-testpubweb
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/css/site.css
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/css/site.css'. Physical path: 'C:srcstatic-testpubwebwwwrootcsssite.css'

As you can see it will successfully serve the css file within a subfolder correctly. Please try the above steps and compare the code and output with your failing project. If it is still failing, please attach the debug info on Request vs. Physical path from Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware above.

这篇关于Asp.Net Core 2.0-2.2 Kestrel 不提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆