Search code examples
asp.net-mvcurlpermalinks

Storing permalinks in the database or building on-the-fly


I was going to ask this on Meta but I think it's a general enough question to warrant a place here instead.

I'm interested in knowing some of the ways you manage permalinks in your site, specifically permalinks that are built from data that can change over time.

StackOverflow is a good example of this whereby the URL to a question is partly made up from the question title. Without posting a dud question to test I'm unsure whether the link to the question changes if the title of the question changes. My guess is that it doesn't and if it does, a canonical is likely retained to the origional url.

Changing the title on SO does not change the url

Given that as the case is it common practice to store permalinks against posts in your database? and if so, how much of the permalink would you store?

I ask the latter because there's only one part of the URL that's variable in the context of SO, and that's the question title. So should we store only the sanitized title and build up the rest based on the static information we have from the post, or should we store the whole url including the controller name and Id (etc.)?


Solution

  • I think it's relatively common to store the permalink in the database. Space is cheap and string parsing functions can be expensive (making a question title HTTP friendly a few thousand times across thousands of questions will eat some processor) each time you want to display the link.

    As for how much to store, personally, I would only store the HTTP friendly version of your question/post title in the DB (along with a primary key) for the following reasons.

    1. Storing the entire or even part of the URL that concerns itself with Actions and Controllers will make it really, really hard to refactor/rename those things down the road. You would either need to run mass DB updates or custom URL rewrites, etc.

    2. Only storing the friendly version of the title allows you to use it in other places. Let's take this URL to this question for example, it was probably generated by @Html.ActionLink(Question.Title, "Index", new {controller = "Questions", Id = Question.Id, Slug = Question.Slug}). Keeping the slug as a separate parameter, you can use the questionId and questionSlug parameters in other controller/action calls and keep your URLs pretty.