ardalis / OrganizingAspNetCore

Offers several different ways to organize content in ASP.NET Core MVC and Razor Pages projects.
http://ardalis.com
MIT License
209 stars 60 forks source link

How do you serve static files ? #13

Closed valeriob closed 6 years ago

valeriob commented 6 years ago

I think a feature folder is not complete with some static content, how do you serve it without copying to wwwroot ? I'm following this approach https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?tabs=aspnetcore2x#serve-static-files

app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")), RequestPath = "/StaticFiles" });

But it only works if the file system path starts with a different char than the request path or it will not allow to have any routing parameter in the request.

ardalis commented 6 years ago

I typically serve static content from wwwroot. If I wanted to keep it in a feature folder organizationally, I would use a build task to copy the files into wwwroot, I think. For apps with significant client complexity (e.g. large Angular SPA), you're likely going to have an entire app folder with its own feature folders representing client features anyway, located in wwwroot.

valeriob commented 6 years ago

Thanks @ardalis Have you done features folder in production with static files or it's just an idea ?

ardalis commented 6 years ago

I haven't, honestly. But I am working with several clients building Angular SPAs and we're definitely using essentially feature folders within the Angular clients.

valeriob commented 6 years ago

So basically you are using features folders concept to organize angular client code, correct ? But from aspnet core point of view it's still a spa, isn't it ?

ardalis commented 6 years ago

That's right. I only mention it because in my experience I'm seeing parallel sets of folder structures on the server and client side. There may be some overlap, but often the client side has different file needs than the server side does, so it makes sense to me to not try to organize client files into server-side feature folders.

At the end of the day, they're really different apps (client vs. server) and could be organized into completely different projects, etc. if you wanted. For simpler (e.g. non-SPA) apps, it might make sense to keep the files in the server-side feature folders (and probably copy them into wwwroot), but that's not the first approach I would try.

Also realize that wwwroot itself is a security feature, because it ensures that files in the rest of your project folder are not at risk of download. If you start adding your own way for requests to reach non-wwwroot folders, you're potentially opening up security risks (like, an attacker might be able to use your feature to access appsettings.json or something else they shouldn't be able to get). Just be careful of that. That's one reason I would prefer to copy into wwwroot rather than giving requests access outside of it.

valeriob commented 6 years ago

Thanks !