Forum Moderators: open

Message Too Old, No Replies

Generating Meta Tags

Generate Meta Tags on individual pages on a precompiled site

         

Weirfire

5:27 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



My project requires that I generate different meta tags on each individual ASP page.

The site uses the inherit function to call the master file and due to the fact the site is precompiled this makes the task a little more difficult.

I thought the tutorial on this page might help [codeproject.com...]

but I hit a dead end when my site returned an error because you can't use the App_Code folder on a precompiled site.

My ASP is fairly basic so if you can give me any help with this it would be hugely appreciated.

marcel

9:27 pm on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Weirfire,

To achieve this you will need to have access to the source code of your compiled web application.

If you have this you can add the new BasePage class anywhere in your solution (the App_Code folder is only necessary when you have a Web Site instead of a Web Application). After adding the BasePage class you can then allow all of your pages to inherit from this.

Hopefully this helps you out, if I have misunderstood let me know.

Weirfire

9:49 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



Thank you for responding.

Can you indicate whether there is any other solution apart from retrieving the source code? The original developers are no longer contactable for which reasons I do not know.

marcel

10:05 pm on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try decompiling it with Reflector [red-gate.com].

I've never used this to fully decompile a web application, but I assume it would be possible (although it would probably be a lot of work)

* EDIT: Maybe in combination with FileDisassembler, found here [codeplex.com]

Weirfire

12:09 pm on Jul 4, 2009 (gmt 0)

10+ Year Member



I wondered whether there would be a solution by editing the master file with some sort of switch case statement based on the server page name variable?

marcel

5:04 pm on Jul 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't understand, you have stated that you do not have access to the source code, but you are planning to add code to the source of the Master Page?

If the site has been compiled this is impossible, unless you manage to somehow decompile the assemblies.

Ocean10000

1:20 am on Jul 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is possible to do this in the Master page. If the html source is not compiled into the DLL. All it would take is a bit of inline code in the Master Page files.

It could look something along these lines.

<script runat="server" Language="C#">
System.Web.UI.HtmlControls.HtmlGenericControl Meta;
string Keywords = string.Empty;
string Description = string.Empty;

//----------------------------------------------------------
//Logic to retrieve the proper Keywords and Description go here.
//----------------------------------------------------------
//Fill in the proper coding here. (Most likely DB Calls.)

//----------------------------------------------------------
//Add Meta Controls to the Header.
//----------------------------------------------------------
if (Keywords!=string.Empty)
{
Meta = new HtmlGenericControl("meta");
Meta.Attributes.Add("content", Keywords);
Meta.Attributes.Add("name", "KEYWORDS");
this.Page.Header.Controls.Add(Meta);
}
if (Description!=string.Empty)
{
Meta = new HtmlGenericControl("meta");
Meta.Attributes.Add("content", Description);
Meta.Attributes.Add("name", "DESCRIPTION");
this.Page.Header.Controls.Add(Meta);
}
</script>

[edited by: Ocean10000 at 1:21 am (utc) on July 9, 2009]

Weirfire

8:44 am on Jul 11, 2009 (gmt 0)

10+ Year Member



Thanks for your reply to my thread about managing the meta tags on a precompiled site.

The master file on my site hasn't been precompiled (as far as I can tell) and I was wondering if you could give some further clarification how I might control the meta tags from within the individual pages. The precompiled sections on this particular site seem to be seperate from the graphical interface as I can still edit this for all pages. I want to be able to control the meta tags from within each individual page using variables within the code. I would be happy to do this via the master file if it's not possible via each seperate page.

I'm more of a PHP coder which is why I'm a bit slow with the basics.

marcel

8:50 am on Jul 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The master file on my site hasn't been precompiled

My apologies, I assumed (always a dangerous thing to do...) that it was also compiled, this is the default setting for pre-compilation.

Could you post the code you have available for the MasterPage and for one of the content pages? (edit out the particulars if you can, we don't need the html markup)

Weirfire

1:45 pm on Jul 11, 2009 (gmt 0)

10+ Year Member



Sure - here is the master file

<%@ master language="C#" autoeventwireup="true" inherits="DDMaster, App_Web_ddmaster.master.cdcab7d2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>{snipped}</title>
<meta name="keywords" content="{snipped}" />
<meta name="description" content="{snipped}" />
<link href="App_Themes/Default/Default.css" rel="stylesheet" type="text/css" />
<link href="App_Themes/Default/print.css" rel="stylesheet" media="print" type="text/css" />
<link href="favicon.ico" rel="Shortcut Icon" />
<script type="text/javascript" src="{snipped}"></script>
</head>

and here is the lettings page;


<%@ page language="C#" masterpagefile="~/DDMaster.master" autoeventwireup="true" inherits="Lettings, App_Web_lettings.aspx.cdcab7d2" title="{snipped}" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
{snipped}
</asp:Content>

Ocean10000

5:02 pm on Jul 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this a Content Management System which has the meta description and keywords being collected for the pages already but not inserting them?

Or will the meta description and keywords be more of a manual thing set on a page by page bases?

Weirfire

9:33 pm on Jul 11, 2009 (gmt 0)

10+ Year Member



The meta description and keywords will be more of a manual thing set on a page by page basis - thanks!

Ocean10000

3:58 am on Jul 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Example The Script Block can be used on any page, including ones with master pages. It will check first that there is a header object present before trying to add meta tags and updating the Title.


<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Sample Page
</div>
</form>
</body>
</html>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl Meta;
string Keywords = string.Empty;
string Description = string.Empty;
string Title = string.Empty;

//----------------------------------------------------------
//Makes sure there is a Header Defined with runat="server"
//And will skip it if it is not defined.
//----------------------------------------------------------
if (this.Page.Header!=null)
{
//----------------------------------------------------------
//Logic to retrieve the proper Keywords and Description go here.
//----------------------------------------------------------
Keywords = "Some,Keywords";
Description= "Description";
Title = "Title Goes Here";

//----------------------------------------------------------
//Simply Adds a Title.
//----------------------------------------------------------
if (Title!=string.Empty)
{
Page.Header.Title = Title;
}

//----------------------------------------------------------
//Add Meta Controls to the Header.
//----------------------------------------------------------
if (Keywords!=string.Empty)
{
Meta = new HtmlGenericControl("meta");
Meta.Attributes.Add("content", Keywords);
Meta.Attributes.Add("name", "KEYWORDS");
this.Page.Header.Controls.Add(Meta);
}
if (Description!=string.Empty)
{
Meta = new HtmlGenericControl("meta");
Meta.Attributes.Add("content", Description);
Meta.Attributes.Add("name", "DESCRIPTION");
this.Page.Header.Controls.Add(Meta);
}
}
}
</script>

Weirfire

8:44 pm on Jul 16, 2009 (gmt 0)

10+ Year Member



Thank you thank you thank you!

That worked perfectly! :)