How to open a JS file in Blazor?

Viewed 136

In my _Host.cshtml file, I have a <script src="/_content/JS/Example.js>, linking to a simple JS file that has the line; console.log('test').

When I check the console of my browser, I receive the error; localhost:5000/_content/JS/Example.js net::ERR_ABORTED 404 (Not Found). Blazor does not seem to be able to find my JS file. For reference the file is located in the wwwroot folder of the BlazorApp, in a folder called JS.

Is there something wrong with my syntax? I would like to be able to access functions located within JSfiles in future,

2 Answers

Use relative paths, where the paths are relative to the wwwroot

<script src="~/JS/Example.js"></script>

Your JS file needs to be inside of the wwwroot folder. In your _Host.cshtml file, also make sure the path to include the file is relative. If the _content folder is an immediate child folder of wwwroot you would do something like this:

<script src="~/_content/JS/Example.js" />
Related