【问题标题】:Cannot access contents of wwwroot folder in Google App Engine无法访问 Google App Engine 中 wwwroot 文件夹的内容
【发布时间】:2020-12-24 13:54:35
【问题描述】:

我有一个在 Visual Studio for Mac 上构建的相当标准的 .net 核心 MVC 应用程序。我已向 Azure 应用服务发布了可能类似的项目。

我现在正尝试将此 MVC 应用程序发布到 Google App Engine。它发布并运行,但浏览器无法访问 wwwroot 文件夹中的任何静态内容 - 所有文件路径都返回 404。

我已确认正在将文件复制到服务器。通过 SSH 我运行 sudo docker exec -it gaeapp /bin/bash

在我登陆的目录中,我看到以下内容:

DnsClient.dll                              
Dockerfile                                 
... (project dlls)
app.yaml
appsettings.json
bundleconfig.json
publish
runtimes
source-context.json
web.config

在发布目录中我看到:

DnsClient.dll
... (project dlls)
app.yaml
appsettings.json
bundleconfig.json
runtimes
web.config
wwwroot

wwwroot 包含我期望的所有文件。我尝试将文件从 wwwroot 复制到树的各个级别,但仍然无法访问这些文件。

这是我的 app.yaml 以防万一:

runtime: aspnetcore
env: flex

# This sample incurs costs to run on the App Engine flexible environment. 
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10
env_variables:
  # The __ in My__Greeting will be translated to a : by ASP.NET.
  My__Greeting: Hello AppEngine!root@c2d2b26894e2:/app# 

有什么想法可以解决这个问题吗?

更多信息

如果我的项目出现问题,我从 VS 模板创建了一个新的 MVC 项目并发布了它。同样的问题,如下面的截图所示。尝试使用默认模板,并按照答案中的建议添加了app.UseDefaultFiles();

所以,我认为问题出在 Google 或部署中...

【问题讨论】:

    标签: asp.net-mvc asp.net-core google-app-engine google-cloud-platform


    【解决方案1】:

    为了让应用引擎灵活地为 dotnet 提供静态文件,您必须通过以下方式允许它:

    app.UseDefaultFiles();
    app.UseStaticFiles();
    

    在您的主文件上。这记录在here

    如果您动态生成文件并将它们放在wwwroot 文件夹中,我不建议这样做,因为一个实例可能会生成文件,但如果另一个实例唤醒,则可能在那里找不到该文件。对于这种情况,我建议使用Cloud Storage 作为 CDN 的替代方案

    【讨论】:

    • 谢谢你;我已经看过这个(虽然没有提及)。我的应用已经有 UseStaticFiles - 遗憾的是添加 UseDefaultFiles 并没有解决我的问题
    • 您是如何尝试访问您的文件的?我的意思是您使用的大概网址是什么?
    • 直接在预期的Url处;还可以在浏览器网络控制台中看到;示例网址:https://....appspot.com/css/site.min.css
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2020-10-14
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多