0

After publishing a .net Core Razor Pages project using a self-contained osx-64 profile and then trying to run it on a Macintosh I noticed that all the static files which reside in the wwwroot folder do not work. They simply return a blank page. There may be same issue when creating an pure MVC (not Razor Pages) site.

Here are the contents of the Startup.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication1
{
    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.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc();
        }
    }
}
adinas
  • 4,150
  • 3
  • 37
  • 47

1 Answers1

1

Apparently, when using app.UseStaticFiles(); on OSX, the location of wwwroot is being looked for elsewhere on the computer instead of in the root of the folder where the app is running.

To solve this I used the following code inside of the

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

function:

if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
   app.UseStaticFiles(new StaticFileOptions
       {FileProvider = new PhysicalFileProvider(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "wwwroot")),RequestPath = ""});
   else
       app.UseStaticFiles();

This will tell OSX to set wwwroot folder in the applications folder as the static files folder. In Windows, the standard app.UseStaticFiles() is used.

adinas
  • 4,150
  • 3
  • 37
  • 47
  • That shouldn't be required. You have either found a bug that has never been reported previously (not likely) or there's some other configuration issue at play that this merely works around. You should post the contents of your `Startup.cs`. – Chris Pratt Mar 11 '19 at 17:43
  • Hi @ChrisPratt, To check that it wasn't something I did, I created an "ASP.NET Core Web Application" project from scratch. Selected "Web Application" in the Wizard and immediately published and still had the same issue. So the Startup.cs file and all other files where created by the VS Wizard. I'd be happy if someone could follow the same path and see if they reproduce the issue. I've added my Startup.cs just prior to changing the code to what I wrote in the answer – adinas Mar 11 '19 at 18:02