<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductMetaService.ascx.cs" Inherits="DesktopModules_ClarityEcommerce_MetaService_ProductMetaService" %>
using System;
using System.IO;
using System.Net;
using System.Web;
using DotNetNuke.UI.Skins;
using Newtonsoft.Json;
// ReSharper disable once InconsistentNaming
public partial class DesktopModules_ClarityEcommerce_MetaService_ProductMetaService : SkinObjectBase
{
protected void Page_Load(object sender, EventArgs e)
{
SetMetadataTitle();
}
private void SetMetadataTitle()
{
var originalRequestedUrlString = Context.Items["UrlRewrite:OriginalUrl"] as string;
if (string.IsNullOrWhiteSpace(originalRequestedUrlString)) { return; }
var originalRequestUrl = new Uri(originalRequestedUrlString);
var path = originalRequestUrl.PathAndQuery;
var seoStart = path.IndexOf("/Product", StringComparison.InvariantCultureIgnoreCase) + "/Product/".Length;
if (seoStart < 0) { return; }
var seoUrl = path.Substring(seoStart);
if (string.IsNullOrWhiteSpace(seoUrl)) { /*Response.Redirect("/", true);*/ return; }
if (seoUrl.Contains("?")) { seoUrl = seoUrl.Substring(0, seoUrl.IndexOf("?")); }
// Get site url (could be different in dev environment)
var siteUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/");
var endPoint = @siteUrl + "DesktopModules/ClarityEcommerce/API/Product/URL?format=json";
var parameters = "{\"SEOURL\":\"" + seoUrl + "\"}";
var request = WebRequest.Create(endPoint);
var data = System.Text.Encoding.UTF8.GetBytes(parameters);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
// ReSharper disable once AssignNullToNotNullAttribute
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
if (string.IsNullOrWhiteSpace(responseString)) { return; }
var productModel = JsonConvert.DeserializeObject<Clarity.Ecommerce.Models.ProductModel>(responseString);
var hasPageTitle = !string.IsNullOrWhiteSpace(productModel.SeoPageTitle);
var hasMetaDescription = !string.IsNullOrWhiteSpace(productModel.SeoDescription);
var name = hasPageTitle ? productModel.SeoPageTitle : (productModel.Name + " | " + PortalSettings.PortalName);
var desc = hasMetaDescription ? productModel.SeoDescription : productModel.ShortDescription;
var keys = productModel.SeoKeywords;
// Create and add meta tag for page title so "ShareThis" code can set the content, inside ajax call is too late for ShareThis to pick up
((DotNetNuke.Framework.CDefault)Page).Title = name;
((DotNetNuke.Framework.CDefault)Page).Description = desc;
((DotNetNuke.Framework.CDefault)Page).KeyWords = keys;
}
}
<%@ Register TagPrefix="cef" TagName="ProductMetaService" Src="~/DesktopModules/ClarityEcommerce/MetaService/ProductMetaService.ascx" %>
<cef:ProductMetaService id="cefProductMetaService" runat="server" />