找不到Asp.net Core 2.0静态文件错误 [英] Asp.net Core 2.0 Static files not found error

查看:49
本文介绍了找不到Asp.net Core 2.0静态文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个ASP.net Core 2.0 Web应用程序;我已将静态文件放置在名为Content的文件夹中.

I am trying to create an ASP.net Core 2.0 web application; I have placed the static files in a folder called Content.

我给出了以下路径:

<link href="@Url.Content("../Content/css/style.css")" rel="stylesheet">

加载视图时出现404-找不到错误.但是,GET请求路径正确,并且文件存在于同一路径中.这是执行GET请求的路径:

I am getting a 404 - not found error when loading the view. However the GET request path is correct and the file exists in the same path. This is the path to which the GET request is being done:

http://localhost:49933/Content/css/style.css 

我找到了解决方案,要求我更改web.config文件中的设置,但是.Net Core 2.0没有.我用过MVC.web.config文件对IIS来说不是很重要吗?

I found solutions that requires me to change settings in web.config file, But .Net Core 2.0 does not have one. I have used MVC. Isn't web.config file very important for IIS?

推荐答案

您应将希望公开的静态文件放在 wwwroot 下,然后相应地引用它们,例如

You should place the static files you wish to expose under wwwroot and then reference them accordingly e.g.

请参见ASP.NET Core中的静态文件

<link rel="stylesheet" href="~/css/style.css" asp-append-version="true" />

如果要在 wwwroot 之外提供静态文件,则需要按以下方式配置静态文件中间件:

If you want to serve static files outside of the wwwroot then you will need to configure static file middleware as follows:

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles(); // For the wwwroot folder

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "Content")),
        RequestPath = "/Content"
    });
}

然后,您可以使用与当前相似的标记:

You can then use similar markup to what you have currently:

<link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet">

请参见

See Serve files outside of web root for more information.

这篇关于找不到Asp.net Core 2.0静态文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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