SEO best practices - prevent 404 error when using dynamically generated URL parameters

Note: This solution is valid for Sitefinity 4+ versions and was tested recently on Sitefinity 8.0

When building custom controls in Sitefinity that will require the usage of URL parameters, you will face the 404 error as result.

Example:

Imagine that you are building a custom Events widget. The events detail view is displayed by URL parameter the following way:

[domain]/about-us/education/detail/[event-name]

Out of the box, Sitefinity URL routing is built so that the PageRouteHandler in Sitefinity will try to search for a page with the same URL and will throw 404 as such page won't exist.

Solution

To prevent the 404 error status code, use the SetUrlParametersResolved method by the RouteHelper as shown below:

private void InitializeDetailsView(IList<EventModel> eventList)
    {
        string itemUrl = string.Empty;
        //gets a portion of the URL specific for the content item
        itemUrl = this.GetUrlParameterString(true);

        if (!string.IsNullOrEmpty(itemUrl))
        {
            //retrieves the eventItem by URL
            var eventItem = eventList.Where(e => itemUrl.Contains(e.EventTitle.ToLower(), UrlRegex, "-"))).FirstOrDefault();

            if (eventItem != null)
            {
                //notifies the PageRouteHandler that the URL parameters are resolved
                RouteHelper.SetUrlParametersResolved();
            }
       }
    }

public static string UrlRegex = @"[^\w\-\!\$\'\(\)\=\@\d_]+";

Please note that the method above is called by the InitializeControls method of the custom events control.

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus