1

Not sure if it is any optimal solution, but lets say I have url like this

?id=2

The Id is taken from database, how do I make it to show something like

/<article_title_name>/

instead of id.

I've been googling for few hours already, and I couldn't really find out how could I retrieve article name while it would still shows the chosen article with targeted id but url woud be just different.

Not sure if I did explained it any well ._ .

Harugawa
  • 539
  • 5
  • 19

1 Answers1

0

Here's how I would do slug links:

This should make your life just that little bit easier, rather than having to reverse-magic some wizardry into matching the string into the post's title just to find the ID.

Here goes...

Have another property on your model:
Create another column/property in your DB table/model for your Articles, called sluglink or something of your choosing, just as meaningful.

Create a method to generate them:
When you save your articles to the DB/repository etc., have a function that can create the link for you and store it in that field.

That way, you haven't got to reverse-engineer magic strings into IDs; you can just find the matching slug in the database/store etc. and return it that way.

There's a great function to do that with (thanks to this post) - the post itself gives the method when creating extension methods; I've just adapted it and condensed it into one function for brevity, in case you're not sure on extension methods:

    public string GenerateSlug(string postTitle)
{
    var str = System.Text.Encoding.ASCII.GetString(System.Text.Encoding
                                        .GetEncoding("Cyrillic")
                                        .GetBytes(postTitle));

    // invalid chars           
    str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); 
    // convert multiple spaces into one space   
    str = Regex.Replace(str, @"\s+", " ").Trim(); 
    // cut and trim 
    str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();   
    str = Regex.Replace(str, @"\s", "-"); // hyphens   
    return str;
}

It's up to you where you want to put this function; I'll let you decide.

Generate and store the slug:
Call the GenerateSlug method, passing your PostTitle or whatever you want to "slugify"; and it will return you a nice-looking string for your "slug".

Then store that string into your database/store etc.

Retrieving the article
Pretty easy, in all honesty.

In your routing table, you can then translate this back to your WebForm to go off and find the correct article. Here's a good link on how to do that: http://weblogs.asp.net/scottgu/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series

Then, in virtually the same way you would GetArticleById(1234), from your repository/store; I would create another method called - guess what... GetArticleBySlug(heres-a-great-article) - or again, anything you like.

Lastly
For the sake of being pure, I'd still have the option to do it by ID, in case you're debugging or whatever the case (saves typing out the entire slug if it's a long article name).

This should get you up and running and on your way to some great looking slugs :)

Hope this helps!

Community
  • 1
  • 1
Geoff James
  • 3,122
  • 1
  • 17
  • 36