Wie implementiere ich URL-Rewriting generisch in einer MapRoute-Methode?


Ich versuche, URLs von C # 's Pascal-Fall zu SEO-freundlichem Format umzuschreiben.
Zum Beispiel möchte ich so etwas/User/Home/MyJumbledPageName so aussehen:

/user/home/my-jumbled-page-name // lower-case, and words separated by dashes


Hier ist meine Methode zum Konvertieren jedes "Tokens" in der URL:

public static string GetSEOFriendlyToken(string token)
{
    StringBuilder str = new StringBuilder();

    for (int i = 0, len = token.Length; i < len; i++)
    {
        if (i == 0)
        {
            // setting the first capital char to lower-case:
            str.Append(Char.ToLower(token[i]));
        }
        else if (Char.IsUpper(token[i]))
        {
            // setting any other capital char to lower-case, preceded by a dash:
            str.Append("-" + Char.ToLower(token[i]));
        }
        else
        {
            str.Append(token[i]);
        }
    }
    return str.ToString();
}


... und in meinemRouteConfig.cs Datei im Stammverzeichnis habe ich diese Routen zugeordnet:

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

    // without this the first URL is blank:
    routes.MapRoute(
        name: "Default_Home",
        url: "index", // hard-coded?? it works...
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Home",
        // the method calls here do not seem to have any effect:
        url: GetSEOFriendlyToken("{action}") + "/" + GetSEOFriendlyToken("{id}"),
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}


Mit diesem Code kann eine URL wie/AboutTheAuthor istnicht umgewandelt, was ich will, was wäre/about-the-author.

Es scheint, dass mein Methodenaufruf ignoriert wird. Was passiert hier? Und wie lässt sich das konventionell umsetzen?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage