A guide on incorporating external scripts and image links into the master page of an "ASP.NET empty web site" using Visual Studio 2019

When working on my project, I encountered an issue with calling external scripts, img links, and related resources in the HomeMaster page. My project structure includes a Home.aspx page within the homepage folder of an asp.net empty web site created using VS2019. However, when running the Home.aspx page, the css, js, and img links are not loading properly.

This is the structure of my project:

https://i.sstatic.net/xizuA.png

In the HomeMaster page, I call the scripts and img links as follows:

In the header

<head runat="server">
   <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Path</title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="../assets/img/favicon.png" rel="icon">
  <link href="../assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="../assets/vendor/aos/aos.css" rel="stylesheet">
  ... (Other link imports)

  <!-- Main CSS File -->
  <link href="../assets/css/style.css" rel="stylesheet">

    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

After the footer content in the body, I call the related js files:

<body>
    <form id="form1" runat="server">
    <div>

      <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
     </asp:ContentPlaceHolder>

  <!-- Vendor JS Files -->
  <script src="../assets/vendor/purecounter/purecounter_vanilla.js"></script>
  ... (Other script imports)

  <!-- Main JS File -->
  <script src="../assets/js/main.js"></script>

    </div>
    </form>
</body>

Additionally, in the Home.aspx page, I call the img links as shown below:

<%@ Page Title="" Language="C#" MasterPageFile="~/HomeMaster.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="homepage_Home" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
... (Image links including client-1.png to client-6.png)
</asp:Content>

Despite no errors occurring while running the Home.aspx page, the images fail to display. This troubleshooting process is new to me, so any guidance on identifying mistakes and resolving them would be greatly appreciated.

Please lend a helping hand!

Answer №1

Alright, let's talk about your master page.

Your master page is located in the root directory, so there's no need to navigate down a folder level like you were doing before.

For example:

<script src="../assets/vendor/purecounter/purecounter_vanilla.js"></script>

Should actually be written as:

<script src="/assets/vendor/purecounter/purecounter_vanilla.js"></script>

(Assuming the above code snippet is in your master page).

You can easily test this out by opening your master page and dragging the JavaScript file from its respective folder into the master page.

For instance:

<html>
<head runat="server">
    <title></title>


    <script src="/Scripts/Test.js"></script>


    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server"gt;
        <div>
                    <h3>My Master page part</h3>
        <br />

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

Notice how the path is referenced from the "master" page itself.

So, the JavaScript function included within the master page is defined as follows:

function myhello() {

    alert("Hello")

}

Now, even if you add a new web page (with the same master) in a nested folder:

https://i.sstatic.net/WjIi7.png

You can still use the myhello() function in your code without any issues.

For example:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

        <asp:Button runat="server" Text="Button"
            OnClientClick="myhello();return false" />

</asp:Content>

Therefore, it's important that your script references start from the root just like the master page does.

Any page, including nested ones, can now freely utilize the myhello() function we declared in the master page regardless of their location.

However, if you want only specific pages to access certain resources on a given web page (whether with a master or not), then relative path names will come into play.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Link the Sass variable to Vue props

When working on creating reusable components in Vue.js, I often utilize Sass variables for maintaining consistency in colors, sizes, and other styles. However, I recently encountered an issue with passing Sass variables using props in Vue.js. If I directly ...

Deleting an List Item from an Unordered List using Angular

Within my MVC controller, I have an unordered list with a button on each item that allows the user to remove it from the list. The issue I'm facing is that while the row is deleted from the database, it still remains visible on the screen. This is ho ...

Guide to displaying a spherical grid helper in Three JS

Currently, I am immersed in a fascinating project that involves the movement of small objects and displaying them within a 360-degree image using the ThreeJS library. To achieve this, I am utilizing the concept of a Spherical coordinate system within a sph ...

Is it possible to use AJAX to target and retrieve multiple first children within an element using .children(":first-child")

I'm facing a simple but puzzling issue - every time I use .children(":first-child"), I get multiple results displayed in consecutive alerts. // ... $.post(ajaxurl, data, function (response) { //alert(response); ...

What is the best way to showcase a lengthy webpage similar to that of dzone.com?

Upon visiting dzone.com, it is evident that all the posts are displayed on a single page. As we scroll down the page, older posts are dynamically added to the end of the page while posts at the beginning are potentially removed from view. I am curious abo ...

The integration of RequireJS with THREE.js Orbit Controls

Currently, I am facing an issue with THREEjs r83 in a requirejs setup. The problem is that the OrbitControl loads and runs before THREEjs initializes, resulting in a persistent error: Uncaught ReferenceError: THREE is not defined In the beginning of my ...

How to reach the Twitter share iframe with JavaScript/HTML

There seems to be an issue with the Twitter share button not displaying at its full width on certain pages. Upon investigation, I discovered that the iframe containing it is only set to a width of 24px, when it should actually be set to the correct width. ...

Changing from system mode to dark mode or light mode

Within my Next.js web application, I am implementing MUI to facilitate the transition between system, light, and dark modes. Persistence between sessions is achieved by storing the selected theme in local storage. The user has the option to change the them ...

Bootstrap 4 fails to adapt on mobile devices, causing columns to pile on top of each other

My website is optimized for desktop viewing, but I'm struggling to make it responsive on mobile using bootstrap 4. Even though my custom columns have always worked in the past, they are now overlapping on top of each other instead of stacking correct ...

How to reference an image located in one folder from a page in a different folder using React

Imagine there is Folder A containing an image called 'image.jpg' and Folder B with an HTML page that needs to access the image in Folder A in order to display it. The following code successfully accomplishes this task: <img src={require(&apo ...

Is combining the use of dapper and MongoDB in a single application considered best practice?

Currently, I am working on developing a BPM (business process management system) using asp.net C# and workflow foundation 4.5. In the typical BPM system, there are numerous processes, each containing multiple forms. To simplify this, I have utilized JSON ...

Steps to Change the Background Color to White in a Dropdown menu

Hello everyone! I'm currently using this codepen example on my website. My query is regarding the fifth panel - is it possible to change the color of the drop-down box when clicking on it? Here's a snippet of the HTML: <link href='https: ...

Avoid the issue of text overlapping in table cell due to specified width

Is there a way to prevent the <td> entry from stretching across the entire screen when it becomes too lengthy? ...

How come the margin-bottom is displaying as the margin-top?

When I attempt to add margin-bottom on a div inside its parent, it seems to display as the parent's margin-top. Why is that? HTML: <div id="wrapper"> <div id="content"> <div id="box"></div> </div> </d ...

Is it possible to access multiple domains within the same IIS server using the same ASPNETDB members database for login?

Currently, I am in the process of developing two websites (2 IIS sites) on a single Windows Server 2003 server that are utilizing a shared ASPNETDB members database. This database was automatically generated by Visual Studio during the integration of a log ...

I'm stuck trying to figure out all the parameters for the MapsPage component in Angular 2

Currently, I am utilizing Angular2 with Ionic2 for my mobile app development. Everything was working flawlessly until I decided to incorporate a new module for Google Maps navigation. Specifically, I am using phonegap-launch-navigator for this purpose. The ...

Use JavaScript to clear the content of the text input field

I encountered an issue with a textbox I created to validate U.S. cellphone numbers using a JavaScript function that only allows numbers. The problem arises when I try to clear the textbox after entering an incorrect number, as the JS function prevents me f ...

Having difficulty injecting $timeout into an AngularJS controller within the app

Recently, I've started working with Angular JS on a game development project. One of the requirements is to call a function after a timeout. After some research, I found out about the $timeout feature provided by AngularJS, so I tried injecting it int ...

The background image is functioning correctly, but there seems to be an issue with the image overlay

I attempted to design a profile page for my website users to have an image as a banner and their profile photo. I have achieved this: https://i.sstatic.net/kFlrQ.jpg However, when I apply the top-margin to the image, it both moves the image and expands t ...

What is the best method for retrieving logs from one Cloud Function to another?

I am attempting to access and review the logs produced by my cloud functions. After reviewing the Firebase Documentation, I discovered a CLI command that allows us to accomplish this task: firebase functions:log This particular command displays all of the ...