/// <summary> /// This method initialize the meta tags properties definition for current initial state of the current page. /// </summary> /// <param name="model"></param> /// <param name="disableDefaultDecoratedTags">Disable default decorated meta tags for the parent controller.</param> /// <returns></returns> protected virtual async Task <MetaTagsModel> InitializeMetaTagsAsync(InitialStateModel <TViewModel> model, bool disableDefaultDecoratedTags = false) { try { var seoOptionsAccessor = (IOptions <DefinuxSeoOptions>) this.HttpContext.RequestServices.GetService(typeof(IOptions <DefinuxSeoOptions>)); var seoOptions = seoOptionsAccessor?.Value; var metaTagsModel = this.ViewData.GetMetaTagsModelOrDefault(); if (metaTagsModel == null) { metaTagsModel = new MetaTagsModel(); } metaTagsModel.ApplyStaticTags(seoOptions.DefaultMetaTags); metaTagsModel.OpenGraphImage.Value = seoOptions.DefaultMetaTags.OpenGraphImage.Value; metaTagsModel.TwitterImage.Value = seoOptions.DefaultMetaTags.TwitterImage.Value; if (!disableDefaultDecoratedTags) { string pageKey = StringFunctions.ConvertToKey(this.GetType().Name); this.AddTranslatedValueIntoViewData(EmPagesConstants.PageMetaTagTitleKey, $"{pageKey}_META_TITLE"); this.AddTranslatedValueIntoViewData(EmPagesConstants.PageMetaTagDescriptionKey, $"{pageKey}_META_DESCRIPTION"); } return(metaTagsModel); } catch (Exception ex) { await this.Logger.LogErrorAsync(ex); return(new MetaTagsModel()); } }
/// <summary> /// Gets or creates <see cref="MetaTagsModel"/> into the ViewData. /// </summary> /// <param name="viewData"></param> /// <returns></returns> public static MetaTagsModel GetOrCreateCurrentMetaTagsModel(this ViewDataDictionary viewData) { MetaTagsModel result = new MetaTagsModel(); try { if (viewData.ContainsKey(ViewDataMetaTagsModelKey) && viewData[ViewDataMetaTagsModelKey] != null) { result = (MetaTagsModel)viewData[ViewDataMetaTagsModelKey]; } } catch (Exception) { result = new MetaTagsModel(); } finally { viewData[ViewDataMetaTagsModelKey] = result; } return(result); }
protected override void InnerProcess(ref TagHelperContext context, ref TagHelperOutput output) { this.ValidateTagHelperAttributesExistence(); output.TagName = this.OutputTagName; output.TagMode = TagMode.StartTagAndEndTag; (this.HtmlHelper as IViewContextAware)?.Contextualize(this.ViewContext); string baseUrl = $"{this.ViewContext.HttpContext.Request.Scheme}://{this.ViewContext.HttpContext.Request.Host}"; string queryString = this.ViewContext.HttpContext.Request.QueryString.HasValue ? this.ViewContext.HttpContext.Request.QueryString.Value : string.Empty; string currentUrl = $"{baseUrl}{this.ViewContext.HttpContext.Request.Path}{queryString}"; MetaTagsModel model = new MetaTagsModel(); model.SetTitle(this.Title); model.SetDescription(this.Description); if (!string.IsNullOrWhiteSpace(this.Image)) { model.SetImage($"{baseUrl}/{this.Image}"); } model.Keywords = this.Keywords; model.OpenGraphUrl = currentUrl; model.Canonical = currentUrl; model.OpenGraphSiteName = this.configuration?["MetaTags:OpenGraphSiteName"]; model.OpenGraphType = this.configuration?["MetaTags:OpenGraphType"]; model.FacebookAppId = this.configuration?["MetaTags:FacebookAppId"]; model.TwitterCard = this.configuration?["MetaTags:TwitterCard"]; model.TwitterCreator = this.configuration?["MetaTags:TwitterCreator"]; model.TwitterSite = this.configuration?["MetaTags:TwitterSite"]; Task <IHtmlContent> htmlContentTask = this.HtmlHelper.PartialAsync(this.PartialPath, model); htmlContentTask.Wait(); output.Content.SetHtmlContent(htmlContentTask.Result); }
/// <summary> /// Apply specified <see cref="MetaTagsModel"/> to the ViewData. /// </summary> /// <param name="viewData"></param> /// <param name="model"></param> public static void ApplyMetaTagsModel(this ViewDataDictionary viewData, MetaTagsModel model) { viewData[ViewDataMetaTagsModelKey] = model; }
/// <summary> /// Initializes a new instance of the <see cref="SeoMetaTagsTagHelper"/> class. /// </summary> /// <param name="optionsAccessor"></param> public SeoMetaTagsTagHelper(IOptions <DefinuxSeoOptions> optionsAccessor) { this.defaultMetaTagsModel = optionsAccessor.Value.DefaultMetaTags; }