Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:05:43+00:00 2026-06-07T16:05:43+00:00

I would like to create custom slugs for pages in my CMS, so users

  • 0

I would like to create custom slugs for pages in my CMS, so users can create their own SEO-urls (like WordPress).

I used to do this in Ruby on Rails and PHP frameworks by “abusing” the 404 route. This route was called when the requested controller could not be found, enabling me te route the user to my dynamic pages controller to parse the slug (From where I redirected them to the real 404 if no page was found). This way the database was only queried to check the requested slug.

However, in MVC the catch-all route is only called when the route does not fit the default route of /{controller}/{action}/{id}.

To still be able to parse custom slugs I modified the RouteConfig.cs file:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        RegisterCustomRoutes(routes);

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { Controller = "Pages", Action = "Index", id = UrlParameter.Optional }
        );
    }

    public static void RegisterCustomRoutes(RouteCollection routes)
    {
        CMSContext db = new CMSContext();
        List<Page> pages = db.Pages.ToList();
        foreach (Page p in pages)
        {
            routes.MapRoute(
                name: p.Title,
                url: p.Slug,
                defaults: new { Controller = "Pages", Action = "Show", id = p.ID }
            );
        }
        db.Dispose();
    }
}

This solves my problem, but requires the Pages table to be fully queried for every request. Because a overloaded show method (public ViewResult Show(Page p)) did not work I also have to retrieve the page a second time because I can only pass the page ID.

  1. Is there a better way to solve my problem?
  2. Is it possible to pass the Page object to my Show method instead of the page ID?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:05:46+00:00Added an answer on June 7, 2026 at 4:05 pm

    Even if your route registration code works as is, the problem will be that the routes are registered statically only on startup. What happens when a new post is added – would you have to restart the app pool?

    You could register a route that contains the SEO slug part of your URL, and then use the slug in a lookup.

    RouteConfig.cs

    routes.MapRoute(
        name: "SeoSlugPageLookup",
        url: "Page/{slug}",
        defaults: new { controller = "Page", 
                        action = "SlugLookup",
                      });
    

    PageController.cs

    public ActionResult SlugLookup (string slug)
    {
        // TODO: Check for null/empty slug here.
    
        int? id = GetPageId (slug);
    
        if (id != null) {    
            return View ("Show", new { id });
        }
    
        // TODO: The fallback should help the user by searching your site for the slug.
        throw new HttpException (404, "NotFound");
    }
    
    private int? GetPageId (string slug)
    {
        int? id = GetPageIdFromCache (slug);
    
        if (id == null) {
            id = GetPageIdFromDatabase (slug);
    
            if (id != null) {
                SetPageIdInCache (slug, id);
            }
        }
    
        return id;
    }
    
    private int? GetPageIdFromCache (string slug)
    {
        // There are many caching techniques for example:
        // http://msdn.microsoft.com/en-us/library/dd287191.aspx
        // http://alandjackson.wordpress.com/2012/04/17/key-based-cache-in-mvc3-5/
        // Depending on how advanced you want your CMS to be,
        // caching could be done in a service layer.
        return slugToPageIdCache.ContainsKey (slug) ? slugToPageIdCache [slug] : null;
    }
    
    private int? SetPageIdInCache (string slug, int id)
    {
        return slugToPageIdCache.GetOrAdd (slug, id);
    }
    
    private int? GetPageIdFromDatabase (string slug)
    {
        using (CMSContext db = new CMSContext()) {
            // Assumes unique slugs.
            Page page = db.Pages.Where (p => p.Slug == requestContext.Url).SingleOrDefault ();
    
            if (page != null) {
                return page.Id;
            }
        }
    
        return null;
    }
    
    public ActionResult Show (int id)
    {
        // Your existing implementation.
    }
    

    (FYI: Code not compiled nor tested – haven’t got my dev environment available right now. Treat it as pseudocode 😉

    This implementation will have one search for the slug per server restart. You could also pre-populate the key-value slug-to-id cache at startup, so all existing page lookups will be cheap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create custom SEO friendly routes similar to what is used
I would like to create my own custom NSMutableArray of my custom objects: @interface
I would like to create a custom CMS within Codeigniter, and I need a
I would like to create my own custom annotation. My framework is stand alone
I would like to create a custom control in order to display a pie
I would like to create a custom document library where I use the standard
I would like to create a custom control in my Android App. It will
I would like to create a custom data type which basically behaves like an
I have authored some custom classes that I would like to create using XAML:
I would like create a custom DataRow that will have -let's say- a propery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.