我们的数据库中有一系列元标记值,需要根据所服务的网站传递给Site.master。如何在页面加载事件中包含BingMetaTag数据库字段值?我们需要将内容属性值从数据库传递到母版页中的元标记。
Page_Load如下:
public partial class Site : System.Web.UI.MasterPage {
protected DealerInformation objDealerInformation = null;
protected DealerSite objDealerSite = null;
protected ConnectionStringConfig dbConfig = null;
protected void Page_Load(object sender, EventArgs e) {
dbConfig = Session["DBConfig" + Request.Url.Host] as ConnectionStringConfig;
objDealerInformation = CommonFunctions.GetDealerInformation(dbConfig);
objDealerSite = Session["DealerSite" + Request.Url.Host] as DealerSite;
try {
imgGoogleAdServices.Src = string.Format(@"//googleads.g.doubleclick.net/pagead/viewthroughconversion/{0}/?value=0&label={1}&guid=ON&script=0;", objDealerSite.GoogleConversionID, objDealerSite.GoogleConversionLabelRemarketing);
} catch {
imgGoogleAdServices.Src = "";
}
try {
WebEntitiesModel context = new WebEntitiesModel(dbConfig["WebConnection"]);
String aspPage = HttpContext.Current.Request.Url.AbsolutePath.ToString().ToLower();
MetaTag pageMetaTag = (from m in context.MetaTags
where m.Page == aspPage
select m).Single();
Page.Title = (pageMetaTag.PageTitle != null ? pageMetaTag.PageTitle : "");
Page.MetaKeywords = (pageMetaTag.MetaKeywords != null ? pageMetaTag.MetaKeywords : "");
Page.MetaDescription = (pageMetaTag.MetaDescription != null ? pageMetaTag.MetaDescription : "");
} catch {
Page.Title = "";
Page.MetaKeywords = "";
Page.MetaDescription = "";
}
请注意,我们已经从其他数据库表中引入了title,meta关键字和元描述。我们只需要将BingMetaTag的值添加为单独的元标记条目。
答案 0 :(得分:2)
听起来你正在使用WebForms而不是MVC,这实际上使这更容易。
在您的Site.master文件中:
<head>
<meta id="someMeta" runat="server" name="something" value="" />
</head>
在Site.master.cs文件的类中:
protected HtmlGenericControl someMeta;
public String SomeMetaValue {
get { return this.someMeta.Attributes["value"]; }
set { this.someMeta.Attributes["value"] = value; }
}
在您网页的课程中
public void Page_Load(Object sender, EventArgs e) {
SiteMaster master = (SiteMaster)this.Master;
master.SomeMetaValue = "someValueFromDatabase";
}
如果项目中有多种类型的母版页,则此代码将失败,因此请根据需要添加适当的警卫和检查。