Best way to turn a post header into a url in C #

I was wondering which of the best ways to turn a string (such as a message header) into a descriptive URL. the easiest way that comes to mind is to use a regular expression, for example:

public static Regex regex = new Regex( "\\W+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled ); string result = regex.Replace(InputText,"_"); 

which turns

"my first (not yet very bad) cupcake !!:) .// \."

in

 my_first_yet_not_so_bad_cupcake_ 

then I can delete the last "_" and check it against my db and see if it is still present. in this case, I would add a finite number to make it unique and double-check.

I could use it, say

 http://myblogsite.xom/posts/my_first_yet_not_so_bad_cupcake 

but is it safe? I have to check other things (like string length) is there any other, better method that you prefer? thanks

+4
source share
6 answers

That's what I'm doing. regStripNonAlpha removes all non-alpha characters or "-". Trim () removes trailing and leading spaces (therefore, we do not end dashes on both sides). regSpaceToDash converts spaces (or runs) into a single dash. It worked well for me.

 static Regex regStripNonAlpha = new Regex(@"[^\w\s\-]+", RegexOptions.Compiled); static Regex regSpaceToDash = new Regex(@"[\s]+", RegexOptions.Compiled); public static string MakeUrlCompatible(string title) { return regSpaceToDash.Replace( regStripNonAlpha.Replace(title, string.Empty).Trim(), "-"); } 
+2
source

string result = regex.Replace (InputText, "-");

instead of subtitling, put hypen (-), which will give an additional advantage for the Google search engine.

See below for more details.

http://www.mattcutts.com/blog/dashes-vs-underscores/

+1
source

Here is a method that I wrote not so long ago that takes a string and formats it to a permalink.

  private string FormatPermalink(string title) { StringBuilder result = new StringBuilder(); title = title.Trim(); bool lastOneChanged = false; for (int i = 0; i < title.Length; i++) { char c = title[i]; if (!char.IsLetterOrDigit(c)) { c = '_'; if (lastOneChanged) { continue; } lastOneChanged = true; } else { lastOneChanged = false; } result.Append(c); } if (result[result.Length - 1] == '_') //if last one is underscore, remove { result = result.Remove(result.Length - 1, 1); } return result.ToString(); } 

This also takes into account special characters, so if the title has a special character, it just ignores it and moves on to the next.

+1
source

You can see the URL rewriting the HTTPModule. There are many examples on the net.

After embedding in your web.config, you simply specify a regular expression to display on the "real" page using a friendly SEO name

 <!-- Rule 1: example... "/admin/somepage" redirects to..."/UI/Forms/Admin/frmPage.aspx" --> <add key="^/admin/(.*)" value="/UI/Forms/Admin/frm$1.aspx" /> 
0
source

If you want to avoid this yourself, an HttpModule, for example http://urlrewriter.net/ can help. This is pretty good, but requires a bit of customization.

0
source

Personally, I will associate your special character with a date so that your example looks like this:

http: //myblogsite.xom/posts/2009/04/03/my_first_yet_not_so_bad_cupcake

Thus, if you are content with the same heading, it also differentiates by date. I often see this on some blogs that I visit, where they often use β€œFive random things make a message” (but not on the same day).

0
source

All Articles