aspnet / StaticFiles

[Archived] Middleware for handling requests for file system resources including files and directories. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
114 stars 72 forks source link

After updating ( 1.1 to 2.0 ) the StaticFiles can't be found in the root path. #210

Closed Eilon closed 6 years ago

Eilon commented 7 years ago

From @NMSLanX on August 16, 2017 7:59

Question:

1、
project: webapi core1.1 nuget: Microsoft.AspNetCore.StaticFiles file: wwwroot/Index.html ok: "http://xxxxx/Index.html" return "hello world" operation: right-click "Controller Folder" --> "Add(D)" --> "controller(T)" --> select any and "Add". error: "http://xxxxx/Index.html" return "http://xxxxx/Index.html/".

2、
project: webapi core 1.1 nuget: Microsoft.AspNetCore.StaticFiles file: wwwroot/Index.html ok: "http://xxxxx/Index.html" return "hello world" operation: update to core2.0 error: "http://xxxxx/Index.html" return "http://xxxxx/Index.html/".

Copied from original issue: aspnet/Mvc#6665

Eilon commented 7 years ago

From @davidfowl on August 16, 2017 8:0

Can you provide a sample that worked before that now fails? Preferably a github repository.

Eilon commented 7 years ago

From @NMSLanX on August 16, 2017 9:6

Hi David: Here is the github repository. #6665.

Eilon commented 7 years ago

From @davidfowl on August 16, 2017 10:14

Just for future reference so you know how to diagnose these issues. Turn logging on to a high verbosity level like trace or debug:

Change this line

https://github.com/NMSLanX/WebApi_6665/blob/3f2a5c8603e0803efa6c328a8b8d878a4d189d59/Error/Actual/WebApiOne/WebApiOne/Startup.cs#L46

To:

loggerFactory.AddConsole(LogLevel.Trace);

The log immediately shows the problem:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:64780/Index.html/
dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[5]
      The request path /Index.html/index.html does not match an existing file

I ran the WebApiOne project under Error. It seems like it 404s without the upgrade to 2.0, are you sure it's related?

Also, I'm not sure this has anything to do with MVC.

Tratcher commented 7 years ago

That trailing slash should not be there: http://localhost:64780/Index.html/ Who is sending that request?

davidfowl commented 7 years ago

@Tratcher it seems to happen automagically. The application is pretty vanilla. Try running it locally.

Tratcher commented 7 years ago

The parameter you're passing in isn't intended to be a default file name, it's intended to be a url segment. .UseDefaultFiles("/Index.html") means you only handle default files for urls starting with "/Index.html/" (but it very helpfully will do a redirect to add that trailing slash for you). If I remove that parameter from your sample it works fine, index.html is already one of the default file names. https://github.com/aspnet/StaticFiles/blob/5d2b1000f19dc3e892611f387b46cd519976a9f0/src/Microsoft.AspNetCore.StaticFiles/DefaultFilesMiddleware.cs#L82

That said, I'm not aware of any changes to this code for 2.0, 1.1 should have the same behavior.

Tratcher commented 7 years ago

While confusing, everything here is behaving as intended. Your main issue is that UseDefaultFiles and UseStaticFiles aren't using the same url path. The following works:

                .UseDefaultFiles("/Index.html")
                .UseStaticFiles("/Index.html")
NMSAzulX commented 7 years ago

Thanks a lot. :)

davidfowl commented 7 years ago

@Tratcher should we improve the docs?

Tratcher commented 7 years ago

Perhaps, though this is the first time I've seen someone do this in almost 5 years.

davidfowl commented 7 years ago

@Tratcher not many people used the katana static file server https://www.nuget.org/packages/Microsoft.Owin.StaticFiles/. Look at the downloads.

aspnet-hello commented 6 years ago

This issue was moved to aspnet/Home#2446