最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Location of JavaScript files in ASP.NET Core Areas - Stack Overflow

programmeradmin11浏览0评论

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

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

Share Improve this question asked Apr 6, 2017 at 13:37 AlexanderAlexander 1,0616 gold badges21 silver badges38 bronze badges 1
  • 1 I found plete response in this post: [ASP.Net MVC Core: Javascript files in Areas along with Views (.cschtml) ](taimooradilbadshah.blogspot./2017/05/…) – Sayed Abolfazl Fatemi Commented Jun 29, 2019 at 13:01
Add a ment  | 

2 Answers 2

Reset to default 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 core where your content files are located. Way to tell asp 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.

The use of server-based solutions will not work in a stand-alone JavaScript file. I use the following:

function getBaseURL() {
    var re = new RegExp(/^.*\//);
    return re.exec(window.location.href);
}

This seems to work both locally and when deployed.

发布评论

评论列表(0)

  1. 暂无评论