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

jquery - Add JavaScript Reference in asp.net core - Stack Overflow

programmeradmin3浏览0评论

Below is my Problem :

GlobalJs.js is my custome JavaScript file and has only one line code alert(1).

But I am unable to add its reference in _Layout.cshtml.

src="../JavaScript/GlobalJs.js" or src="~/JavaScript/GlobalJs.js" none is working for me. What is the problem actually? Please anyone help me. Am I missing something?

Thanks in advance.

Below is my Problem :

GlobalJs.js is my custome JavaScript file and has only one line code alert(1).

But I am unable to add its reference in _Layout.cshtml.

src="../JavaScript/GlobalJs.js" or src="~/JavaScript/GlobalJs.js" none is working for me. What is the problem actually? Please anyone help me. Am I missing something?

Thanks in advance.

Share Improve this question asked Feb 20, 2017 at 9:48 user7364287user7364287
Add a comment  | 

3 Answers 3

Reset to default 17

In ASP.NET Core, static files such as javacript, css etc are serve to web browser from wwwroot folder.

Hence, you need to move these files into wwwroot folder and then reference files in _Layout.cshtml using <script src="~/JavaScript/GlobalJs.js"></script>

For more information, please refer this article

In ASP.NET Core, Static files, such as HTML, CSS, image, and JavaScript, are assets that serve from wwwroot folder to web browser.

Hence, you need to move these files into wwwroot folder and then reference files in _Layout.cshtml using <script src="~/JavaScript/GlobalJs.js"></script>

Note: In order for static files to be served, you must configure the Middleware to add static files to the pipeline. The static file middleware can be configured by adding a dependency on the Microsoft.AspNetCore.StaticFiles package to your project and then calling the UseStaticFiles extension method from Startup.Configure

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();
}

I came here with the same problem but my solution was to double check that the javascript file was copy to the solution. Easy to forget this configuration, right click on the file and select properties:

发布评论

评论列表(0)

  1. 暂无评论