resources not found with asp.net core 3.1 + linux nginx (static files problem?)

2k views Asked by At

I'm trying to run an app asp.net core 3.1 MVC on my raspberry pi 4. The app with its resources files (img, css...) works fine on IIS debug mode (windows).

I followed this tutorial to set up the linux environment : tuto The app starts well in linux but no resource on pages.

So I checked microsoft doc pages about Static files : Static Files and others tuorials. Mostly, they explain how to change the resources directory path but this is not my problem. I just would like see it works with wwwroot first.

I didn't changed anything in startup.cs as you can see :

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();

        app.UseRouting();

        app.UseAuthorization();

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

Neither in program.cs :

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

nginx config file content :

server {
    listen        80 default_server;
    server_name   _;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

Do you have any idea, please, how to fix it? Thanks for reading this :)

3

There are 3 answers

2
AdrienM87 On BEST ANSWER

Problem solved : static files were expected at /home/user instead of where I setted up the app

0
DRised On

Configure your .service file like this:

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/home/username/dotnet/application_folder
ExecStart=/usr/bin/dotnet /home/username/dotnet/application_folder/bin/Debug/net5.0/application.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
0
HansHeinz On

I had the same problem, the static files were always searched in the wrong folder. Config changes in Blazor or in Nginx didn't help, no difference occurred.

Like AdrienM87 stated, the solution was to call the executable in it's own folder - in my case /var/www/html.