Location of JavaScript files in ASP.NET Core Areas

喜夏-厌秋 提交于 2019-12-07 16:00:36

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!