Esempio n. 1
0
        /// <summary>
        /// Method that create the initial state model - the customized data + all generic data. Overriding is not recommended.
        /// </summary>
        /// <param name="dataQuery"></param>
        /// <returns></returns>
        protected virtual async Task <InitialStateModel <TViewModel> > CreateInitialStateModelAsync(IRequest <TViewModel> dataQuery)
        {
            var model = new InitialStateModel <TViewModel>(this.GetType().Name);

            var currentLanguage = await this.CurrentLanguageProvider.GetCurrentLanguageAsync();

            var currentUser = await this.CurrentUserProvider.GetCurrentUserAsync();

            model.User.IsAuthenticated = this.User.Identity.IsAuthenticated;
            model.LanguageCode         = currentLanguage?.Code;
            model.LanguageId           = currentLanguage?.Id ?? -1;

            if (model.User.IsAuthenticated && currentUser != null)
            {
                model.User.Roles = this.User.Claims.Where(x => x.Type == ClaimTypes.Role).Select(x => x.Value).ToArray();
                model.User.Email = currentUser.Email;
                model.User.Name  = currentUser.Name;
            }

            model.ViewModel = dataQuery is null ? null : await this.Mediator.Send(dataQuery);

            var viewDataItems = await this.InitializeViewDataAsync(model);

            if (viewDataItems != null && viewDataItems.Count > 0)
            {
                foreach (var(key, value) in viewDataItems)
                {
                    model.AddViewDataItem(key, value);
                }
            }

            model.MetaTags = await this.InitializeMetaTagsAsync(model);

            return(model);
        }
Esempio n. 2
0
        /// <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());
            }
        }
Esempio n. 3
0
        protected override Task <MetaTagsModel> InitializeMetaTagsAsync(InitialStateModel <DogsViewModel> model, bool disableDefaultDecoratedTags = false)
        {
            AddTranslatedValueIntoViewData(EmPagesConstants.PageMetaTagTitleKey, "DOGS");
            AddTranslatedValueIntoViewData(EmPagesConstants.PageMetaTagDescriptionKey, "DOGS");

            return(base.InitializeMetaTagsAsync(model, disableDefaultDecoratedTags));
        }
Esempio n. 4
0
 /// <summary>
 /// This method initializes the key-value pair used as view data for the initial state after the page view model is computed but before the page sending action result to the view.
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 protected virtual async Task <Dictionary <string, object> > InitializeViewDataAsync(InitialStateModel <TViewModel> model)
 {
     return(new Dictionary <string, object>());
 }
Esempio n. 5
0
 protected IActionResult EmPageView(InitialStateModel <TViewModel> model)
 {
     return(this.View(this.EmPageViewName, model));
 }
Esempio n. 6
0
 protected async override Task <Dictionary <string, object> > InitializeViewDataAsync(InitialStateModel <DogsViewModel> model)
 {
     return(new Dictionary <string, object>
     {
         { "SomeToken", Guid.NewGuid() }
     });
 }