Search code examples
javascriptasp.net-coreareas

Location of JavaScript files in ASP.NET Core Areas


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?)?


Solution

  • "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.