Friday, June 16, 2017

ASP.NET: include javascript file in asp.net page

1. add the javascript file (for example, test.js) into the script folder

2. update BundleConfig.cs to create a new bundle for the js file
     bundles.Add(new ScriptBundle("~/bundles/test").Include(
                                  "~/Scripts/test.js"));


3. to add the js file for all views, update _layout.cshtml in <head> section
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/test")
</head>

or simple add the below line in any cshtml view
    @Scripts.Render("~/bundles/test")


No comments:

Post a Comment