Kentico MVC 12 - working example of Articles widget

enterprise procurement asked on January 18, 2022 20:31

Can anyone share a working example of Articles widget that is using PageAlias.

Thanks.

Recent Answers


Liam Goldfinch answered on January 18, 2022 21:06

What does this articles widget need to do? Does it just need to output a list of article cards?

If so, there's an example of how to achieve that in the Dancing Goat sample website. I would recommend looking at the source code for that for inspiration.

It renders something like this: Image Text

Key files of interest would be:

  • Controller - DancingGoatMvc\Controllers\Widgets\ArticlesWidgetController.cs
  • View - DancingGoatMvc\Views\Shared\Widgets\_ArticlesWidget.cshtml
  • Repository - DancingGoatMvc\Repositories\Implementation\KenticoArticleRepository.cs
4 votesVote for this answer Mark as a Correct answer

T S answered on January 24, 2022 17:49 (last edited on January 24, 2022 17:49)

Where can I download old Dancing Goat sample site? the Kentico 13 version for Dancing goat is bit different.

1 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on January 24, 2022 17:58 (last edited on January 24, 2022 18:00)

If you're wanting to download an older version of Kentico, e.g. Kentico 12, I think you'll need to download the installer (which includes Dancing Goat) from the Client Portal.

I think the public area for downloading Kentico Xperience only offers the current latest version.

If you don't have access to the Client Portal, you could try asking for a Kentico 12 installer through Kentico Support email address - support@kentico.com

1 votesVote for this answer Mark as a Correct answer

T S answered on January 24, 2022 18:11

Thanks so much Liam! appreciate the quick reply, was able to download Kentico 12 via portal, thanks forgot abput that hahahah

So by looking at the Dancing Goat on MVC 12, I see the articles has the GUID ID as part of the URL,I am looking for a way to remove it from the URL for SEO reasons. can you help me out with this?

Cheers!

1 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on January 24, 2022 18:29 (last edited on January 24, 2022 18:32)

No problem.

Like you've said, Kentico 12 Dancing Goat doesn't really have the best practices here, which is why I suggested to take inspiration rather than directly copy.

What you could do is modify ArticleViewModel's GetViewModel method to resolve the URL directly and store against a property on the ViewModel, e.g.

    public string Url { get; set; }


    public static ArticleViewModel GetViewModel(Article article)
    {
        return new ArticleViewModel
        {
            NodeAlias = article.NodeAlias,
            NodeGUID = article.NodeGUID,
            PublicationDate = article.PublicationDate,
            RelatedArticles = article.Fields.RelatedArticles.OfType<Article>().Select(GetViewModel),
            Summary = article.Fields.Summary,
            Teaser = article.Fields.Teaser,
            Text = article.Fields.Text,
            Title = article.Fields.Title,
            Url = URLHelper.ResolveUrl(article.RelativeURL)
        };
    }

Then in the view you can use @article.Url instead of @Url.ForArticle(article.NodeGUID, article.NodeAlias)

Ideally you need to change the Page Type to have a different URL pattern that doesn't use GUID too.

1 votesVote for this answer Mark as a Correct answer

T S answered on January 24, 2022 18:46 (last edited on January 24, 2022 18:46)

So I have tried what you suggested and I get page not found message, I am trying that on the Dancing Goat site first.

And for Article(MVC) page type I have this: /{%DocumentCulture%}/Articles/{%NodeAlias%}

What am I missing here?

1 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on January 24, 2022 18:54

Ah, that is probably because the Dancing Goat site uses specific routes configured in the RouteConfig.cs file.

For example:

        var route = routes.MapRoute(
            name: "Article",
            url: "{culture}/Articles/{guid}/{pageAlias}",
            defaults: new { culture = defaultCulture.Name, controller = "Articles", action = "Show" },
            constraints: new { culture = new SiteCultureConstraint(), guid = new GuidRouteConstraint() }
        );

So you'd need to amend this to match your new pattern, and then amend the Controller as well.

Honestly, instead of trying to use Kentico 12 and your own routing, I would highly recommend using Kentico Xperience 13 and use the out of the box routing. You don't have to worry about URL Patterns and routing to controllers, Kentico Xperience 13 handles it all for you.

1 votesVote for this answer Mark as a Correct answer

T S answered on January 24, 2022 19:11

Thanks again for trying to help, very much appreciated! I figured that's what I need to do, seems like this issue resolved in Kentico 13, since there is no GUID in the URL, unfortantly my company is using this version, so I need to figure a way to fix this annoying issue with the GUID

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.