Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I generically implement URL-rewriting in a MapRoute method?
    primarykey
    data
    text
    <p><br /> I am attempting to rewrite URL's from C#'s Pascal-case to SEO-friendly format. <br /> For example, I want something like <code>/User/Home/MyJumbledPageName</code> to look like this:</p> <pre><code>/user/home/my-jumbled-page-name // lower-case, and words separated by dashes </code></pre> <p><br /> Here is my method for converting each "token" in the URL:</p> <pre><code>public static string GetSEOFriendlyToken(string token) { StringBuilder str = new StringBuilder(); for (int i = 0, len = token.Length; i &lt; 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(); } </code></pre> <p><br /> ...and in my <code>RouteConfig.cs</code> file in the root, I have mapped these routes:</p> <pre><code>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 } ); } </code></pre> <p><br /> With this code, a URL such as <code>/AboutTheAuthor</code> is <strong>not</strong> converted to what I want, which would be <code>/about-the-author</code>. <br /><br /> It seems that my method call is ignored, what's happening here? And what's the conventional way to implement this? <br /></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload