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 user7364287user73642873 Answers
Reset to default 17In 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: