4

I am creating an ASP.NET Core application that will contain several areas. Where would I add JavaScript files that are specific to a certain area (usually I put them into the wwwroot\js Folder. Is there something like this for an area?)?

Alexander
  • 1,021
  • 6
  • 20
  • 38
  • 1
    I found complete response in this post: [ASP.Net MVC Core: Javascript files in Areas along with Views (.cschtml) ](http://taimooradilbadshah.blogspot.com/2017/05/aspnet-mvc-core-javascript-files-in.html) – Sayed Abolfazl Fatemi Jun 29 '19 at 13:01

1 Answers1

4

"Where would I add JavaScript files"? Answer is you can choose your location based on your requirements and convenience. Default location is content root i.e. wwwroot folder and its subfolders however ASP.Net Core doesn't stop you from placing those static files outside "wwwroot" folder. However if you want to place it outside default content folder i.e. wwwroot you need to tell asp.net core where your content files are located. Way to tell asp.net core is configure StaticFiles middleware as follows:

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

Here MyStaticFiles folder is outside wwwroot and located just inside your project directory.

Now if you want to access any file inside MyStaticFiles folder then it will be using following path.

http://<myapp>/StaticFiles/myscript.js

Note "StaticFiles" in above path and in middleware configuration.

Pankaj Kapare
  • 7,486
  • 5
  • 40
  • 56
  • 1
    Is this not a security risk? Would this allow access to non static files, such as reading .cshtml files and .cs files? – Wayne Jan 10 '20 at 02:39