为什么wwwroot文件夹下的js、css、lib没有加载?

发布于 2025-01-18 17:52:05 字数 3172 浏览 0 评论 0原文

我制作了一个简单的ASP MVC(Net Core 3.1),将其作为标准化,并在Apache的子文件夹(/var/www/www/html/html/myapps/test/test/v1`)

I 下部署。启动kestrel而没有任何错误,我访问页面(即https://example.com/myapps/test/v1),但显示了所有CSS,JS,LIB,lib in (err_aborted 404(找不到),我看到它们已经在www-root文件夹中。

bootstrap.min.css
site.css
bootstrap.bundle.min.js
site.js
content.bundle.js
content.js

我想念什么?

试图访问https://example.com/myapps/test/test/v1)) ,它显示了一些CSS,JS,LIB不能正确加载(err_abort 404(找不到),我看到它们已经在www-root文件夹中。

我的startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            **// Have Tried this**
            app.UsePathBase("/myapps/test/v1");

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }

 [Unit]
 Description=.NET Web Application on Linux

 [Service]
 WorkingDirectory=/var/www/html/myapps/test/v1
 ExecStart=/usr/bin/dotnet 
 /var/www/html/myapps/test/v1/WebApplication4.dll
 Restart=always
 # Restart service after 10 seconds if the dotnet service crashes:
 RestartSec=10
 KillSignal=SIGINT
 SyslogIdentifier=dotnet-example
 User=root
 Environment=ASPNETCORE_ENVIRONMENT=Production
 Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

在 下方apache httpd.conf

< virtualHost *:443>

ServerName apps.example.com
ServerAlias apps.example.com
ServerAdmin [email protected]

DocumentRoot /var/www/html

<Directory /var/www/html>
    Options -Indexes +FollowSymLinks
    AllowOverride All
</Directory>

ProxyPreserveHost On

<Location /myapps/test/v1/>
  ProxyPass http://localhost:5003/
  ProxyPassReverse http://localhost:5003/
  Require all granted
</Location>

I made a simple ASP MVC (Net Core 3.1) , I leave it as standard, and deploy it under a subfolder of the Apache (/var/www/html/myapps/test/v1`)

I started the kestrel without any errors, I access the page (ie. https://example.com/myapps/test/v1), it shows but all css, js, lib don't load properly (ERR_ABORTED 404 (Not Found), I see they are already in the www-root folder.

bootstrap.min.css
site.css
bootstrap.bundle.min.js
site.js
content.bundle.js
content.js

what did I miss?

Have tried to access https://example.com/myapps/test/v1), it shows but some css, js, lib don't load properly (ERR_ABORTED 404 (Not Found), I see they are already in the www-root folder.

below my startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            **// Have Tried this**
            app.UsePathBase("/myapps/test/v1");

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }

my kestrel

 [Unit]
 Description=.NET Web Application on Linux

 [Service]
 WorkingDirectory=/var/www/html/myapps/test/v1
 ExecStart=/usr/bin/dotnet 
 /var/www/html/myapps/test/v1/WebApplication4.dll
 Restart=always
 # Restart service after 10 seconds if the dotnet service crashes:
 RestartSec=10
 KillSignal=SIGINT
 SyslogIdentifier=dotnet-example
 User=root
 Environment=ASPNETCORE_ENVIRONMENT=Production
 Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

my apache httpd.conf

<VirtualHost *:443>

ServerName apps.example.com
ServerAlias apps.example.com
ServerAdmin [email protected]

DocumentRoot /var/www/html

<Directory /var/www/html>
    Options -Indexes +FollowSymLinks
    AllowOverride All
</Directory>

ProxyPreserveHost On

<Location /myapps/test/v1/>
  ProxyPass http://localhost:5003/
  ProxyPassReverse http://localhost:5003/
  Require all granted
</Location>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-01-25 17:52:05

如果您将应用程序托管在子路径 /myapps/test/v1 下,那么您将需要配置 ASP.NET Core 应用程序,以便它知道。您可以使用 Configure 方法开头的 UsePathBase 方法来执行此操作:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UsePathBase("/myapps/test/v1");

    // …

这样,Razor 将知道生成应用程序相对路径(例如 ~/bootstrap .min.css) 相对于配置的路径基础。因此,浏览器知道在 /myapps/test/v1/bootstrap.min.css 中查找文件,而不仅仅是 /bootstrap.min.css


在您的 Apache 配置中,尝试设置 ProxyPreserveHost On 以便 Apache 传递真实主机名而不是内部本地主机地址。这应该允许 ASP.NET Core 应用程序识别出它是在外部托管的,并且在 Razor 视图中生成链接时使 UsePathBase 配置生效。

If you host your application under the subpath /myapps/test/v1, then you will need to configure your ASP.NET Core application so that it knows that. You can do that with the UsePathBase method at the beginning of your Configure method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UsePathBase("/myapps/test/v1");

    // …

That way, Razor will know to generate application-relative paths (e.g. ~/bootstrap.min.css) to as relative to that configured path base. So the browser knows to look for the files at /myapps/test/v1/bootstrap.min.css instead of just /bootstrap.min.css.


In your Apache configuration, try to set ProxyPreserveHost On in order to have Apache pass the real host name instead of the internal localhost address. This should allow the ASP.NET Core application to recognize that it is being hosted externally and have the UsePathBase configuration take an effect when generating the links within the Razor views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文