zero_coding zero_coding - 4 months ago 10
ASP.NET (C#) Question

Append javascript file into view

I created an application that based on ASP.NET Core .NET Framework.

I have a view and want to add javascript file on it and did as follow:

@{
ViewData["Title"] = "Index";
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>
<script src="~/js/app.js"></script>
<input id="greet" value="Hello!" type="button"/>


I do not add to the layout template, because it is only for view specific.

The file structure looks as follow:

enter image description here

My problem is, when I start the application, then the javascript file
app.js
does not load at all, also
jquery.backstretch.min.js
file.
What am I doing wrong?

Kld Kld
Answer

Javascript

In your view

@section scripts {
       <script src="~/js/app.js"></script>
}

In your layout

@RenderSection("scripts", required: false)

CSS

in your view:

@section Styles {
    <link href="~/Content/contact.css" rel="stylesheet" type="text/css" />
}

In your layour

@RenderSection("styles", false)