Create short permalinks similar to Stack short permalink to this question

I think I may have already figured out how this works, but I wanted to be sure.

I am in the process of defining routes for a new ASP.NET MVC application. I would like to create short permalinks similar to Stack Overflow, a short link to this question:

Create short permalinks similar to "Stack Overflow" and "short permalink to this question"

What routing and controller mechanism is used for?

Other questions discussing routes of issues:

+5
source share
1 answer

I believe the routes install something similar to this:

routes.MapRoute("question-permalink", "q/{questionId}/{userId}", 
    new { controller = "PermaLinkController",
        action = "Question", userId = UrlParameter.Optional },
    new { questionId = "[0-9]+", userId = "[0-9]+" });

Based on 302 Foundindicating the current location of the question: I assume that the PermaLink Control Action looks something like this:

public class PermaLinkController : Controller
{
    public Question (int questionId, int? userId)
    {
        // do work to record userId that shared link
        // ...
        // now redirect
        Response.RedirectToRoute("question", new { questionId = questionId });
    }
}
+1
source

All Articles