0
我們的數據庫中有一系列元標記值,需要根據提供的站點傳遞給Site.master。如何在我的頁面加載事件中包含BingMetaTag數據庫字段值?我們需要將內容屬性值從數據庫傳遞到母版頁中的元標記。如何從數據庫添加動態元標記到Site.Master?
的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 = "";
}
請注意,我們已經從另一個數據庫錶帶來的標題,meta關鍵字和meta描述。我們只需要將BingMetaTag的值添加爲單獨的元標記條目。
+1,但是在母版頁自己做這件事很容易,所以你只需要做一次。 – MikeSmithDev
在WebForms中,我認爲MasterPage不應該有自己的任何邏輯,Page類應該充當控制器。 – Dai