Alex Zhang
Alex Zhang

Reputation: 1118

asp.net core MVC static file given 404 error when run website from parent directory

I have an incomprehensible bug recently.

I published a ASP.NET Core MVC web application to a folder, when I enter the root path of the website and run it using command

d:\publish> dotnet WebSite.dll

all things works fine.

but when I go to the parent folder of this website, and then use below command to run it.

d:\> dotnet ./publish/WebSite.dll

the web site can visit,but all the static files given a 404 errorenter image description here,

I want to know, why? how can I solve this?

Upvotes: 3

Views: 2829

Answers (1)

Alex Zhang
Alex Zhang

Reputation: 1118

Because UseStaticFiles use the path from env.ContentRootPath, But env.ContentRootPath will take path where you run the dotnet command, so the physical path is getting wrong.

I have reassign the base physical path like this:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider($@"{AppDomain.CurrentDomain.BaseDirectory}/wwwroot")
});

And all static resources are revisited.

Upvotes: 9

Related Questions