我正在创建一个包含多个区域的 ASP.NET Core 应用程序。我将在哪里添加特定于某个区域的 JavaScript 文件(通常我将它们放入 wwwroot\js 文件夹。对于某个区域有类似的东西吗?)?
1 回答
"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.