Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net Core 2.0-2.2红隼不提供静态内容_C#_Css_Asp.net Core_.net Core - Fatal编程技术网

C# Asp.Net Core 2.0-2.2红隼不提供静态内容

C# Asp.Net Core 2.0-2.2红隼不提供静态内容,c#,css,asp.net-core,.net-core,C#,Css,Asp.net Core,.net Core,使用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)中输入文件路径时,文件不会显示,但浏览器似乎重定向

使用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
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?}");
    });
}
//Program.cs
公共静态IWebHost BuildWebHost(字符串[]args)=>WebHost
.CreateDefaultBuilder(args)
.UseStartup()
.Build();
//Startup.cs
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseExceptionHandler(“/Error/HandleError”);
app.UseStaticFiles();
app.UseMvc(路由=>
{
MapRoute(名称:“默认”,模板:“{controller=User}/{action=Index}/{id?}”);
});
}

请检查以下标记中是否存在这些静态文件

<environment include="Development">
</environment>


发布时,如果静态文件存在于
包含开发
中,则它们将不存在于已发布的文件夹中。另外,请检查
查看源代码
,并查看这些链接是否存在。

按照以下步骤,我无法使用新的ASP.NET Core 2.0项目重现您的错误

  • md静态试验
  • cd静态试验
  • dotnet新网站
  • 在wwwroot中添加文件夹
    css
  • wwwroot/css
    中添加文件
    site.css
  • 插入
    app.UseStaticFiles()
    Startup.Configure()方法的开始处
  • dotnetpublish-opubweb
  • cd pubweb
  • dotnet.\static-test.dll
  • 使用浏览器进行访问
  • dotnet.exe
    在我的终端中呈现以下输出:

    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文件。请尝试上述步骤,并将代码和输出与失败的项目进行比较。如果仍然失败,请将调试信息附加到上面Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware的
    请求
    物理路径
    上。

    我刚刚遇到了一个非常类似的问题。我无法让红隼提供任何静态内容;我有一个在.NETCore1.1中运行良好的配置,但在.NETCore2.0中不起作用

    我通过它进行了调试,静态文件中间件使用的IFileProvider实例是NullFileProvider。我必须在Startup.cs中这样做才能获得静态内容:

    app.UseFileServer(
        new FileServerOptions() {
            FileProvider = new PhysicalFileProvider("some_path")
        });
    

    我尝试了很多无用的东西,这是我的解决办法:

    WebHost.CreateDefaultBuilder(args)
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .UseWebRoot("E:\\xyz\\wwwroot")
    .UseUrls("http://localhost:5050")
        .Build();
    
    我的一台服务器在端口5000上运行时发生冲突,因此我指定在端口5050上启动


    .Net Core 2.2的更新

    对于.NET Core 2.2,以下工作:
    .UseWebRoot(“wwwroot”)

    但是,获取路径的更具可读性和明确性的方法是:

    静态字符串webRoot=Path.Combine(AppContext.BaseDirectory,“wwwroot”)

    然后
    UseWebRoot(webRoot)

    经过反复试验,我意识到一旦我开始使用Kestrel,静态文件web根目录就会突然映射到bin文件夹,而不是development文件夹。要解决此问题,您可以执行以下两种操作之一

  • 您可以将要包含的所有静态文件设置为复制到输出目录
  • 您可以将根目录重新映射到特定路径:

    .UseKestrel(...)
    .UseWebRoot(@"C:\MyProject\wwwroot\");
    

  • 我使用我在其中开发的wwwroot文件夹,因此我不必确保所有新项目都添加到复制到输出目录。

    对我来说,问题是工作目录。当我试图用
    dotnet/var/www/project/project.dll
    启动应用程序时,我没有注意我所在的目录。以这种方式启动应用程序时,它会自动将当前目录用作工作目录

    我在查看另一个项目的
    .service
    文件时意识到了这一点,该文件指定了WorkingDirectory:

    ...
    WorkingDirectory=/var/www/project/
    ExecStart=/usr/bin/dotnet /var/www/project/project.dll
    ...
    

    因此,在运行项目时,请确保您位于正确的目录中,或者确保在
    .service
    文件中正确设置了工作目录。

    我已使用asp.net core2.1(MVC)创建了一个新项目使用MS模板,然后包含一个css文件animate.css,其中包含一些动画

    为了能够使用它,必须在\u Layout.cshtml
    因此,我注意到,MS“忘记”添加环境“登台”和“生产” 所以如果在“开发”环境中添加href,则调试(IIS Express)时一切正常,但它不适用于自主机(我假设也不适用于“真正的”IIS),因为这是开发环境和href是在运行时设置的。
    因此,我复制粘贴了“开发”环境,也用于“登台”和“生产”(参见下面的截图)

    
    @*->完全不见了!从开发中粘贴的副本*@
    @*结束手动插入*@
    
    ...
    WorkingDirectory=/var/www/project/
    ExecStart=/usr/bin/dotnet /var/www/project/project.dll
    ...
    
      <environment include="Development">
            <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
            <link rel="stylesheet" href="~/css/site.css" />
            <link rel="stylesheet" href="~/css/animate.css">
        </environment>
        <environment exclude="Development">
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
                  asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
                  asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
            <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        </environment>
    
        @*<environment include="Staging,Production"> -> was missing completely!copy pasted from Development*@
        <environment include="Staging,Production">
            <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
            <link rel="stylesheet" href="~/css/site.css" />
            <link rel="stylesheet" href="~/css/animate.css">
        </environment>
        <environment exclude="Staging,Production">
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
                  asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
                  asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
            <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        </environment>
        @*End manually inserted*@
    
    WORKDIR /app
    # Example publish/build command:
    RUN dotnet publish -o out
    # Your publish folder is /app/out so...
    # ENTRYPOINT ["dotnet", "/app/out/myapp.dll"]
    # will run but you'll get 404s on all your static content. 
    # Instead use:
    WORKDIR /app/out
    ENTRYPOINT ["dotnet", "myapp.dll"]
    
    .UseWebRoot(@"C:\MyProject\wwwroot\");
    
    // Find app directory.
    var isService = !(Debugger.IsAttached || args.Contains("--console"));
    var currentDir = Directory.GetCurrentDirectory();
    if (isService)
    {
        var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
        currentDir = Path.GetDirectoryName(pathToExe);
    }
    
    // Configure web host
    var host = WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseContentRoot(currentDir)
            .UseWebRoot(Path.Combine(currentDir, "wwwroot"));
            .UseStartup<Startup>()
            .Build();
    
    // Run.
    if (isService)
    {
        host.RunAsService();
    }
    else
    {
        host.Run();
    }