发布到 Azure 时,区域中的 ASP.NET Core 文件夹不会加载资源

标签 asp.net asp.net-mvc azure visual-studio asp.net-core

在我的 ASP.Net Core 3.1 项目 (Visual Studio 2019) 中,我创建了一个 Areas 文件夹并添加了一个名为“Admin”的区域。在管理中,我添加了一个名为“assets”的静态文件夹。这包含我的管理区域的所有 CSS、JS、图像文件:

enter image description here

在startup.cs文件中,我添加了以下代码,以便可以链接到 Assets 文件夹中的静态文件:

enter image description here

我从这里获取了该代码:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1#serve-files-outside-of-web-root

View 中的示例链接如下所示:

enter image description here

当我在本地运行项目并浏览到/admin 时,所有资源(CSS、JS、图像等)都会加载,并且网站会正确显示。

但是,当我将项目发布到 azurewebsites.net 时,当我浏览到/admin 时,资源未加载! View 已渲染,但 CSS、JS 和图像链接不起作用。

我已检查服务器, Assets 文件夹和文件位于服务器上。

任何有关如何在 Azure 上实现此功能的帮助或建议将不胜感激!

编辑: 当我在页面上打开 Web 开发工具 (F12) 时,链接显示为 404 错误: enter image description here

“/assets/css/icons.min.css”不存在,但为什么它不像我在本地运行时那样指向/Areas/Admin/assets/css?

编辑: 在 Azure 上检查,似乎只有 .json 文件(以及其中包含 .json 文件的文件夹)正在发布!我直接链接到其中一个 .json 文件并且它浏览正确,因此我需要弄清楚为什么 VS 没有发布所有文件。 enter image description here

最佳答案

Path.Combine(env.ContentRootPath, "Areas/Admin/assets")),RequestPath = "/assets"

  1. 这行代码意味着当您请求“/assets”时,它将映射到“Areas/Admin/assets”。
  2. 根据您提供的图片,请求的路径没有问题。
  3. 但是根据404错误提示,通常会检查请求的文件是否存在。
  4. 您之前提到的文件已经存在
    • 我还建议您检查icons.min.css文件是否确实存在于Areas/Admin/assets/css中强>目录。
  5. 如果您确定这些文件确实存在,我怀疑问题可能是由文件夹权限引起的。

☑编辑

Checking on Azure, it seems as if only the .json files (and folders that have .json files in them) are being published! I linked directly to one of the .json files and it browsed correctly so I need to figure out why VS isn't publishing all the files.

  1. 默认发布 ASP.NET Core Web 应用程序时,包含以下资源:
    1. 构建工件
    2. 与以下通配模式匹配的文件夹和文件:
      • ***.config(例如 web.config)
      • ***.json(例如 appsettings.json)
      • wwwroot**
  2. 您需要在发布时包含所需的文件。

关于发布到 Azure 时,区域中的 ASP.NET Core 文件夹不会加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67905536/

相关文章:

asp.net - 只需发布修改后的 Controller 、模型和 ASP.NET MVC 项目的某些部分

c# - 无法让我的 DropDownListFor 在下拉菜单中选择选定的 SelectListItem 项目

ASP.NET Web API knockout 验证

asp.net - int 溢出时本地化为 "the value {0} is invalid"

c# - 无法将字符串转换为字符

wcf - 在 Windows Azure 中部署 WCF : One or more role instances are unhealthy. 回收..未处理的异常 : System. NullReferenceException?

node.js - 使用 Azure AD 登录我的 Node.js 应用程序后出现隐秘的 "client does not exist"错误

azure - 使用 Azure Function 作为流分析的输出 : Connection Test Fails

c# - 找不到扩展方法

asp.net-mvc - 在 MVC 中定义 html5 数据属性