Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My project is an asp.net webpages website which has a layout including head section of pages. I wana do this:
HTML
<head>
   <link rel="canonical" href="@App.PathHome" />
</head>

but not in layout. I wana add it dynamically via asp.net code in the home page. How is the syntax?
Now I am trying this:
C#
//Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "/Default";
myHtmlLink.Attributes.Add("rel", "canonical");
// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);

but getting the error "Cannot perform runtime binding on a null reference" for the last code line.
Posted
Updated 7-May-13 5:36am
v4
Comments
[no name] 7-May-13 11:01am    
do you want to use something like this ?
<title> <%=pageTitle%></title>
cs101000 7-May-13 11:34am    
No. I am trying to do just the action in the above head element. But because I have a head element in site layout, if I put another in a child page I'd have 2 head elements in that page. So, I am trying to do that through code (and avoid html markup)

1 solution

I'm not sure you can use controls in WebMatrix.
Anyway, maybe you can accomplish your task with the use of sections.
If you add the row
C#
@RenderSection("otherlinks", required: false)

in the head of your site layout page and
C#
@section otherlinks {
    <link rel="canonical" href="@App.PathHome" />
}

in your child page, the resulting page should work.
 
Share this answer
 
Comments
cs101000 8-May-13 5:04am    
Thank you! It worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900