DEV Community

Cover image for SeoEngine for filter the link?
Anna Hajare
Anna Hajare

Posted on

SeoEngine for filter the link?

You absolutely CAN create a small “SEO indexability engine”. And for your use case (?page=2 indexable, ?sort=hhh not indexable), this is actually the best long-term solution.

Below is a clean, production-ready SEO engine for ASP.NET MVC 5.

What this SEO Engine Will Do

  • Decide indexable vs non-indexable
  • Handle pagination, sort, filter, search params
  • Generate canonical URL
  • Control meta robots automatically
  • Centralized (no duplicate logic in views)

SEO RULE ENGINE (Concept)

Parameter Behavior
page Indexable
sort Noindex
filter Noindex
search Noindex

SEO Decision Model

public class SeoResult
{
    public bool IsIndexable { get; set; }
    public string CanonicalUrl { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

SEO Engine (Core Logic)

public static class SeoEngine
{
    private static readonly HashSet<string> NonIndexableParams =
        new HashSet<string>(StringComparer.OrdinalIgnoreCase)
        {
            "sort",
            "filter",
            "search",
            "utm_source",
            "utm_medium"
        };

    public static SeoResult Evaluate(HttpRequestBase request)
    {
        var query = request.QueryString;
        bool hasNonIndexableParam = false;

        foreach (string key in query.Keys)
        {
            if (key == null) continue;

            if (NonIndexableParams.Contains(key))
                hasNonIndexableParam = true;
        }

        var baseUrl = request.Url.GetLeftPart(UriPartial.Path);

        return new SeoResult
        {
            IsIndexable = !hasNonIndexableParam,
            CanonicalUrl = baseUrl
        };
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Works in ASP.NET MVC 5
  • Clean extension syntax
  • No breaking changes

Extension Method for HtmlHelper

public static class SeoEngineExtensions
{
    public static SeoResult Evaluate(this HtmlHelper htmlHelper)
    {
        var request = htmlHelper.ViewContext.HttpContext.Request;
        return SeoEngine.Evaluate(request);
    }
}
Enter fullscreen mode Exit fullscreen mode

Use Engine in Controller

public ActionResult Index(int page = 1, string sort = null)
{
    var seo = SeoEngine.Evaluate(Request);

    ViewBag.CanonicalUrl = seo.CanonicalUrl;
    ViewBag.NoIndex = !seo.IsIndexable;

    ViewBag.Page = page;

    return View();
}
Enter fullscreen mode Exit fullscreen mode

Layout (_Layout.cshtml)

<link rel="canonical" href="@ViewBag.CanonicalUrl" />

@if (ViewBag.NoIndex == true)
{
    <meta name="robots" content="noindex, follow" />
}
else
{
    <meta name="robots" content="index, follow" />
}
Enter fullscreen mode Exit fullscreen mode

What This Engine Achieves

URL Canonical Indexable
/articles /articles Indexable
/articles?page=2 /articles?page=2 Indexable
/articles?sort=asc /articles Noindexable
/articles?sort=asc&page=2 /articles?page=2 Noindexable
/articles?utm_source=fb /articles Noindexable
  • Google-safe
  • No duplicate content
  • Crawl budget preserved

Why This Is the RIGHT Approach

  • Centralized SEO logic
  • Easy to extend
  • No fragile Razor conditions
  • Works with pagination, filters, tracking params
  • Matches Google best practices

Also Read

Top comments (0)