I'm working on a website and I use ASP.NET 5 CLR (rc1-final x64). I want to have my wwwroot folder 'clean'. I feel everything that should exist in wwwroot should be added on build or through tasks (for example through Grunt). I'm using Grunt for this, and this works fine with JavaScript and CSS files.
Now I'm at that point of adding images to my website. Right now my .gitignore says to exclude the wwwroot. I don't know how to move images from outside the wwwroot to inside it. Is there any good way to move files to the wwwroot? I really want my wwwroot 'clean' without any libraries. I have those outside of the wwwroot, so I don't accidentally serve unwanted files.
I want to move them from: [Application Root]/static/img to wwwroot/img.
Currently I have the following grunt tasks:
- grunt-contrib-concat
- grunt-contrib-cssmin
- grunt-contrib-uglify
I think this may be very in-efficient because on build we need to move all the files every time.
Is there any way to do this better / more efficiently?
EDIT #1:
Yeah, so I read the documentation: http://docs.asp.net/en/latest/fundamentals/static-files.html.
I could actually just pass in a parameter to the app.UseStaticFiles()
method like this:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"),
RequestPath = new PathString("/StaticFiles")
});
Here the PhysicalFileProvider
contains the absolute path. Is it possible to use a relative path here?
EDIT #2: Actually this works:
public void Configure(IApplicationBuilder app, IHostingEnvironment environment, ILoggerFactory loggerFactory, IApplicationEnvironment appEnvironment)
{
app.UseIISPlatformHandler();
app.UseDeveloperExceptionPage();
app.UseMvcWithDefaultRoute()
.UseStaticFiles()
.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(string.Concat(appEnvironment.ApplicationBasePath,"/static/images")),
RequestPath = new PathString("/img")
});
}
Here we add the UseStaticFiles
middleware twice to serve the default, which is wwwroot. We will also serve files from within the solution where the images are.
However, I don't know if there would be any performance or security problems or other problems by adding this middleware twice. You need to be careful in what you add in this folder. Directory Browsing should atleast not be enabled.
wwwroot