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

查看:127
本文介绍了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 /目录)。

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).

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

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

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

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.

我想念什么?谢谢

// 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?}");
    });
}


推荐答案

我无法复制您的使用新的ASP.NET Core 2.0项目执行以下步骤时出现错误;

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


  1. md静态测试

  2. cd静态测试

  3. dotnet新网络

  4. 在wwwroot中添加文件夹 css

  5. 在其中添加文件 site.css wwwroot / css

  6. app.UseStaticFiles(); 插入 Startup.Configure()方法的开始。

  7. dotnet发布-o pubweb

  8. cd pubweb

  9. dotnet .\static-test.dll

  10. 使用浏览器访问 http:// localhost:5000 / css / site.css 。 / li>
  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 在我的终端中呈现以下输出:

dotnet.exe renders the following output in my terminal:

Hosting environment: Production
Content root path: C:\src\static-test\pubweb
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:\src\static-test\pubweb\wwwroot\css\site.css'

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

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天全站免登陆