Why doesn’t View see the javascript file?

Asked

Viewed 157 times

1

How to make the view run the javascript file: novo.js

Directory structure:
Views > Product > js > new.js

Error:

inserir a descrição da imagem aqui

View Product.cshtml:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

    @section scripts{

@*<script src="@Url.Content("~/Views/Produto/js/novo.js")" type="text/javascript"></script>*@

        <script src="~/Views/Produto/js/novo.js"></script>
    }

New file.js:

$(function () {
    console.log("olá");
});
  • If you look in the Source tab of your browser devtools the new.js file appears.

  • OK @Thales Morais I will edit the post and improve the question I changed where written see to execute.

  • Okay, I understand your question. The point is that it is necessary to identify whether the file is loading into your browser. Because if it is not there is something wrong in the middleware used for this or something like that. If not the error is on the way to the scipt

  • Important to remember to put middleware in your app.js for the file to identify the static file. I edited my answer to make it clearer

  • @Thales Morais as you can see the example I posted is an execution of a very simple jquery function, the source code is well customized and detail if I put the new.js file in the default folder created by Visual Studio: Content/Scripts works normalment.

3 answers

0


0

add this in webConfig, in my solved

<system.web>
    <httpHandlers>
        <add path="*.js" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
        <add path="*.css" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
        <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <!-- other content here -->
</system.web>

-1

If you are using Node.js with express make sure you have put the new.js as Static file as below:

app.use(express.static(path.join(__dirname, 'js/novo.js')))

And it is important that the file has uploaded to the client’s browser. For this reason, make sure that your new.js is loading into the source tab of your browser’s devtools. If it means that your file is correct and the error is in the view if it doesn’t mean that the error is in your app.js the file is probably not to get statico in the client.

  • thanks but did not resolve

  • I edited the answer to make it clearer

  • Hello Thales Morais thank you but I am not using nodes.js.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.