Knyri
Knyri

Reputation: 3028

Master pages, paths, and a little ContentPlaceHolder

I am completely new to ASP.Net. New to ASP period actually. So forgive my ignorance if it is very obvious.

I used to have my code in the Master page like this but changed it in case the text inside the ContenPlaceHolder would be replaced with whatever the page supplied.

<head Runat="server">
    <title><asp:ContentPlaceHolder ID="title" Runat="server"></asp:ContentPlaceHolder></title>
    <asp:ContentPlaceHolder ID="stylesheets" Runat="server">
        <link rel="stylesheet" type="text/css" href="s/main.css" />
    </asp:ContentPlaceHolder>
    <asp:ContentPlaceHolder ID="scripts" Runat="server">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <!--[if lt IE 9]>
        <script type="text/javascript" src="js/html5.js"></script>
        <![endif]-->
        <script type="text/javascript" src="js/slideshow.js"></script>
    </asp:ContentPlaceHolder>
</head>

It produced:

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="s/main.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="js/slideshow.js"></script>
</head>

Now when I changed it to this it decided it was going to resolve my href attributes.

<head Runat="server">
    <title><asp:ContentPlaceHolder ID="title" Runat="server"></asp:ContentPlaceHolder></title>
    <link rel="stylesheet" type="text/css" href="s/main.css" />
    <asp:ContentPlaceHolder ID="stylesheets" Runat="server"></asp:ContentPlaceHolder>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <asp:ContentPlaceHolder ID="scripts" Runat="server"></asp:ContentPlaceHolder>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="js/slideshow.js"></script>
</head>

and

<head Runat="server">
    <title><asp:ContentPlaceHolder ID="title" Runat="server"></asp:ContentPlaceHolder></title>
    <link rel="stylesheet" type="text/css" href="./s/main.css" />
    <asp:ContentPlaceHolder ID="stylesheets" Runat="server"></asp:ContentPlaceHolder>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <asp:ContentPlaceHolder ID="scripts" Runat="server"></asp:ContentPlaceHolder>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="js/slideshow.js"></script>
</head>

became

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="masters/s/main.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="js/slideshow.js"></script>
</head>

Now, after some reading on here and in my book I came up with this which also is not right.

<head Runat="server">
    <title><asp:ContentPlaceHolder ID="title" Runat="server"></asp:ContentPlaceHolder></title>
    <link rel="stylesheet" type="text/css" href="~/s/main.css" />
    <asp:ContentPlaceHolder ID="stylesheets" Runat="server"></asp:ContentPlaceHolder>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <asp:ContentPlaceHolder ID="scripts" Runat="server"></asp:ContentPlaceHolder>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="js/slideshow.js"></script>
</head>

Produced:

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../s/main.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <script type="text/javascript" src="js/slideshow.js"></script>
</head>

The folder set-up is like so:

/masters/normal-page.master
/index.aspx #uses normal-page.master

Now, the question is how can I disable this behavior or get it to produce href="s/main.css" or an equivalent? Yes the obvious solution is to move normal-page.master to the root, but I don't like cluttering the root folder.

Also, if I had used the original and the page gave it something for that place holder in the page would the text between the place holder tag be replaced or would it be appended? ( the book seems to assume I know all this. )

Also, I am not using Visual Studio and will not be as I cannot justify the cost yet. I am using Notepad++.

Upvotes: 2

Views: 1160

Answers (1)

Jeremy
Jeremy

Reputation: 9030

There is a ResolveUrl method that makes this very simple. Try this out and see if it helps you:

<link href="<%= ResolveUrl("~/s/main.css")%>" rel="stylesheet" type="text/css" />

Some additional reading: Control.ResolveUrl Method

Upvotes: 2

Related Questions