3

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?

3
  • It seem read a wrong file path, when I display the ContentRootPath, It's show the current run command path, D:\. Commented Mar 11, 2019 at 11:28
  • It's likely due to the paths you used. In Razor code you use the ~/ path prefix to specify that it should be relative to the document root. If you use just / at the beginning of your paths, then that will be domain relative, which may or may not be the same thing. Bear in mind that ~/ is non-standard, though, so it only works in things like Razor views, as Razor will extrapolate that for you. If you're referencing paths in something like a JS file, you'll need a different solution. Commented Mar 11, 2019 at 13:02
  • It seems the root path of app.UseStaticFiles using is depend on env.ContentRootPath, because env.ContentRootPath take path where you run the command. so the physical path is getting wrong, I will give out the solution for this case. Thanks a lot. Commented Mar 12, 2019 at 1:07

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.