In the Master Page for Visual Studio 2005 website, I am using meta description as
<meta name="keywords" content="coupons, coupons, online coupons, health deals, health coupons, computer coupons, bargains, hot bargains" />
<meta name="description" content="<%=MyMetaDescription%>" />
I would like to replace the content in each aspx page. How do I use it?
I get the same content as description though I have tried to replace the content in code behind page.
In the code behind, i have called Master.MyMetaDescription = "";
Can someone help me out on this. Thanks a lot in advance!
I would use this :
this.Page.MetaKeywords = "football, sport, golf";
this.Page.MetaDescription = "My site is all about football and golf";
Obviously if you just want to append to them then just use += (don't forget a leading , for the keywords).
Make a content placeholder where the <meta> tag will go in the <head> section:
<html>
<head>
<asp:ContentPlaceHolder ID = "MetaContent" runat = "server" />
</head>
<!-- other content -->
</html>
In your pages then you can replace the content using a different <meta> tag.
<asp:Content ID = "MyMeta" PlaceHolderID = "MetaContent" runat = "server">
<meta />
</asp:Content>
Yuck has already provided a simple enough way to add meta content.
Regardless, see this code project article to see how meta tags can be added from code behind. It also provides a nice solution based on that to easily add meta tags to page.
Related
I'm looking for validation here.
Some initial definitions I use:
"gator tags" are the <% %> tags for executing 'at render' code
Given the following:
<%# Page Language="C#" %>
<html>
<head runat="server">
<!-- meta description --><meta runat="server" id="meta_description" name="description" content="" visible="false" />
<link rel="canonical" href="<%=Context.Request.Url.AbsoluteUri %>" />
</head>
<body>
<h1>ASPNet Head runat=server parser bug.</h1>
<p>Inspect the source and look for <link rel="canonical" and see that you have the Render code enclosed with <% %> being sent to the browser.</p>
</body>
</html>
First, take notice of the render code in the <link rel="canonical" href="<%= Context.Request.AbsoluteUri %>" />. This is intended to output the AbsoluteUri for the current page (please refrain from the distraction about link rel=canonical semantics... I acknowledge this may not be the ideal way to do this)
When you run this code, the HTML sent to the browser is as follows:
<html>
<head>
<!-- meta description --><link rel="canonical" href="<%=Context.Request.Url.AbsoluteUri %>" /><title>
</title></head>
<body>
<h1>ASPNet Head runat=server parser bug.</h1>
<p>Inspect the source and look for <link rel="canonical" and see that you have the Render code enclosed with <% %> being sent to the browser.</p>
</body>
</html>
Notice how the render code within <% %> is output into the HTML stream, and not executed?
The major bug I see is that the ASP.Net parser doesn't adequately kick into Code Parsing state for the link tag, but instead grabs the whole string "<%= Context.Request.Url.AbsoluteUri %>" and shoves that into the (string)Href property for HtmlLink. (You can verify this by adding <%asdf%> somewhere on the page to see the ASP.Net parser's generated source and search for 'AbsoluteUri').
The odd thing is that Head runat=Server seems to kick the ASP.Net parser into a state where every tag, with or without 'runat=server', gets built as a server control. This has several side effects that I've noticed over the years, like whitespace slamming and others.
When you remove runat="server" from the HEAD tag, it works properly. The expected behavior is the Context.Request.Url.AbsoluteUri is properly emitted as: http://localhost:5678/HeadParserBug.aspx
I don't want this to be a discussion, but a validation by stackoverflow community of the problem. Can the ASP.Net community at large validate this find? Thanks.
I have a doubt that in asp.net I am using CssClass class attribute for web server controls. And i am writing all the css code in style.css which will be in style folder of my project
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
The above is the example of the textbox.
Now the question is do i need to use link tag to say that my css file is located in style folder of my project?
Yes, you have to link the style sheet file (.css) by adding the link tag.
You can also simply drag the css file into the html section of the .aspx code right under head tag, that will work too - that will create the link for you.
Of course you need to link to your css file just like you do in regular html:
<link rel="stylesheet" type="text/css" href="style/mystyle.css">
While writing .aspx file just think as if you are writing an HTML file with the ability to pre-process the page through the ASP.NET view engine (which is where the additional asp tags come into play).
No, the tag is only used to apply a class within your CSS file. Just make sure you link your CSS file with your page as follows:
<head>
<link rel="stylesheet" type="text/css" href="path_to_your.css">
</head>
Ok, I'm sure it must be a silly mistake on my part, but I can't find where the problem is, and it's driving me nuts.
I have a master page, with this:
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" /> - Company
</title>
</head>
It's just the default HTML inserted by VS when I created the masterpage, I just added " - Company" at the end, so that I don't have to repeat that text in every single view.
On the views, I have, for example, this:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Some title for the view
</asp:Content>
As you can imagine, the final result is not what I expected. Instead of
<title>Some title for the View - Company</title>
I'm getting:
<title>Some title for the View</title>
Why?
Seems to be a quirk about how Classic ASP.Net (aka WebForms) works. Phil Haacked on Title Tags and Master Pages is a great read.
Although he goes into depth about the reasons it works the way it does, it seems he isn't referring to MVC specifically. The first comment by Erik Porter has the crazy easy solution:
Change
<head runat="server">
to
<head>
Tada, fixed.
Try this inside your title tag:
<asp:ContentPlaceHolder ID="TitleContent" runat="server" /><%= " - Company" %>
I ran into this a while back and putting the literal inside a code block cleared it up. I'm not really sure why, though, if someone has an explanation.
You have closed the title tag twice. Also, try not having the ContentPlaceHolder be self closing.
In MVC, I don't recall ever using the ContentPlaceHolder. I strongly type my master page and populate the master page that way.
I'm trying to programmatically add a <meta>. It is working fine when there is a Head element with runat = "server" in the .aspx page.
The code behind is:
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Header.Controls.Add(meta);
But I have some script in the head tag which contains code blocks like <% ... %>, so I cannot keep the runat = "server" value.
The problem is I have to add the meta tag programmatically, because it depends on a value from the database.
Is there a way to solve this issue so that my script inside the head element works as usual and I can add a meta tag programmatically?
OK, I tested the answer by veggerby, and it works perfectly:
In the <header> section:
<asp:PlaceHolder id="MetaPlaceHolder" runat="server" />
Note that Visual Studio might show a warning on the PlaceHolder tag, because it is not recognised as a known element inside the header, but you can ignore this. It works.
In the C# code:
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
MetaPlaceHolder.Controls.Add(meta);
Alternatively (since you already have code blocks using <% %> in your header section), you can tag the meta directly and retrieve only the value from server side:
<meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />
Many thanks to Awe for the solution! I have implemented this code in a (error404.ascx) ASP.NET User Control as follows:
<%# Control Language="C#"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true; //Suppress IIS7 custom errors
Response.StatusCode = 404;
SetRobotsHeaderMetadata();
}
private void SetRobotsHeaderMetadata()
{
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Master.FindControl("cphPageMetaData").Controls.Add(meta);
}
</script>
With the following masterpage:
<%# Master Language="C#" AutoEventWireup="true" Inherits="MyMaster" %>
<script runat="server">
...
</script>
<!DOCTYPE html>
<html lang="en-GB">
<head>
<title>Site title here</title>
<asp:contentplaceholder runat="server" id="cphPageMetaData">
</asp:contentplaceholder>
</head>
<body>
...
</body>
</html>
Or you could just put your meta-tag in the header, with an ID and a runat="server"... then in the code behind say
myMetaTag.Content = "noindex,follow";
or
myMetaTag.Visible = false;
or whatever you'd like.
I think this is the best approach:
this.Page.Header.Controls.Add(new LiteralControl(#"<meta ... />"));
Enjoy!
Try moving whatever it is that you are doing in the <% .... %> to the code-behind. If you are using the script to add content into the page, you can replace it with an asp:Literal control and then set the value you were previously calculating in the script block to the code-behind and set Literal.Text to that value.
I haven't tested it, but maybe you can add an <asp:Placeholder> inside the <head></head> tag and add the meta tags to this.
The best solution for this, which I successfully checked without any error or warning:
The JavaScript code, which contains the <% ... %> code, was removed from the head section and placed in the body section.
You could define your meta tag as a static string like so:
Private Shared MetaLanguage As String =
String.Format("<meta http-equiv=""Content-Language"" content=""{0}""/>", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
Then place them in your head like so:
<head runat="server">
<%=MetaLanguage%>
</head>
This allow you to use any meta tag values and is easy to read and customize. Note: The use of the Shared keyword (static) helps improve performance.
MetaDescription = "Your meta description goes here";
MetaKeywords = "Keyword1,Keyword2,Keyword3";
OK... I actually only use C#... Or HTML into C#. I never use codebehind, designer or webcontrols in the file aspx... So I program everything from classes... And dynamically.
Result:
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
`meta.Content = "Here is what you want";`
var page=HttpContext.Current.Handler as Page;
page.Header.Controls.Add(meta);
I am using the MVC to add a title to the masterpage with a content place holder. The default MVC masterpage template uses the following code:
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server"/></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
When I try to add defaulted text to the front of the content holder, it doesn't show the text in the final rendered page. I am trying to get the page to show a default title with appended contentplaceholder text.
Example:
(Default Text) (ContentPlaceHolder Text)
My Page - About Us
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My Page - <asp:ContentPlaceHolder ID="TitleContent" runat="server"/></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
I am looking for a way to accomplish this without having to use code behind. Any ideas?
It seems we should use
<asp:Literal runat="server" Text=" - MySite" />
instead of
<asp:LiteralControl runat="server" Text=" - MySite" />
mentioned in the article, because otherwise we get "Unknown server tag" error.
After looking further, Phil Haack actually posted an article which was a solution to my question. It can be found at Haacked.
In summary he said that everything that is rendered in the head is rendered as a control, and the fix for my question above is to put an asp literal control in the title to have it correctly generate the text.
<%# Master ... %>
<html>
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="titleContent" runat="server" />
<asp:LiteralControl runat="server" Text=" - MySite" />
</title>
</head>
...
Why?
<title>
<asp:ContentPlaceHolder ID="titleContent" runat="server" />
<%= "- My Site" %>
</title>
Works just as well. Without the hassle?
I prefer to use this:
<title>Site Name - <%=Page.Title%></title>
Much cleaner than using a literal control..
If you are using MVC and are passing the title in some object from the controller to the page I would use inline code to display this.
We use the MVC contrib functions to get typed data directly from the view data in the master page thus:
<head>
<title>My Page - <%= ViewData.Get<Model.Page>().Title %></title>
</head>
As a point of note we have removed all code behind files from every view we have to make the views more legible, we find this much better than having code behind for each view.