예제 #1
0
        /// <summary>
        /// Identifies SEO meta tag values in the specified response headers and attempts to injects them into
        /// the specified page via content placeholders assumed to exist in the page's master page.
        /// </summary>
        /// <param name="page">A reference to the current page.</param>
        /// <param name="headers">Response headers from an HTTP request.</param>
        public static void InjectSeoMetaTagsIntoPage(HttpResponseHeaders headers)
        {
            if (HttpContext.Current == null)
                return;

            var page = HttpContext.Current.Handler as Page;

            if (page == null || headers == null)
                return;

            // Look for new values within HTTP headers
            IEnumerable<string> seoData = null;

            if (headers.TryGetValues(Constants.SeoFields.SeoData, out seoData))
            {
                // Deserialize new values
                var serializer = new DataContractJsonSerializer(typeof(Dictionary<string, string>));
                var seoValues = (Dictionary<string, string>)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(seoData.First())));

                // Get master page placeholder controls
                var pageTitleControl = page.Master.FindControl(Constants.ContentPlaceholders.PageTitle);
                var seoMetaTagControl = page.Master.FindControl(Constants.ContentPlaceholders.SeoMetaTags);
                var canonicalUrlControl = page.Master.FindControl(Constants.ContentPlaceholders.CanonicalUrlMetaTag);
                var seoH1Control = page.Master.FindControl(Constants.ContentPlaceholders.SeoH1Tag);

                // Inject values into controls
                if (pageTitleControl != null && seoValues.ContainsKey(Constants.SeoFields.PageTitle))
                {
                    pageTitleControl.Controls.Clear();
                    pageTitleControl.Controls.Add(new LiteralControl("<title>" + seoValues[Constants.SeoFields.PageTitle] + "</title>"));
                }

                if (canonicalUrlControl != null && seoValues.ContainsKey(Constants.SeoFields.CanonicalUrl))
                {
                    canonicalUrlControl.Controls.Clear();
                    canonicalUrlControl.Controls.Add(new LiteralControl("<link rel=\"canonical\" href=\"" + seoValues[Constants.SeoFields.CanonicalUrl] + "\"/>"));
                }

                if (seoH1Control != null && seoValues.ContainsKey(Constants.SeoFields.H1))
                {
                    seoH1Control.Controls.Clear();
                    seoH1Control.Controls.Add(new LiteralControl("<h1 class=\"ih-seo-title\" itemprop=\"name\">" + seoValues[Constants.SeoFields.H1] + "</h1>"));
                }

                if (seoMetaTagControl != null)
                {
                    var newMetaTags = CreateSeoMetaTagsFromValues(seoValues);
                    var oldMetaTags = ((LiteralControl)seoMetaTagControl.Controls[0]).Text;
                    var finalMetaTags = MergeSeoMetaTags(newMetaTags, oldMetaTags);

                    seoMetaTagControl.Controls.Clear();
                    seoMetaTagControl.Controls.Add(new LiteralControl(finalMetaTags));
                }
            }
        }
예제 #2
0
        private static long GetHeaderValue(HttpResponseHeaders headers, string name)
        {
            long result = 0;

            IEnumerable<string> values = null;
            headers.TryGetValues(name, out values);
            string value = values?.FirstOrDefault();
            if (value != null)
            {
                long.TryParse(value, out result);
            }

            return result;
        }
        private string GetSettingsVersionHeader(HttpResponseHeaders headers) {
            IEnumerable<string> values;
            if (headers != null && headers.TryGetValues(ExceptionlessHeaders.ConfigurationVersion, out values))
                return values.FirstOrDefault();

            return null;
        }
예제 #4
0
		public void Set(HttpResponseHeaders headers)
		{
			IEnumerable<string> values;

			if (headers.TryGetValues(TwitterDefinitions.HttpResponse.RateLimit, out values))
			{
				var limit = values.FirstOrDefault();

				if (headers.TryGetValues(TwitterDefinitions.HttpResponse.RateLimitRemaining, out values))
				{
					var remaining = values.FirstOrDefault();

					if (headers.TryGetValues(TwitterDefinitions.HttpResponse.RateLimitReset, out values))
					{
						var reset = values.FirstOrDefault();

						if (limit != null && remaining != null && reset != null)
						{
							try
							{
								this.Set(int.Parse(limit), int.Parse(remaining), long.Parse(reset));
							}
							catch (Exception ex)
							{
								ex.Write();
							}
						}
					}
				}
			}
		}