views:

470

answers:

7

I want to create a similar type of url as stack overflow does when a question is created.

Example:

http://stackoverflow.com/questions/1149454/non-ajax-get-post-using-jquery-plugin

I am particularly interested in the last part which I have highlighed in bold. How do you achieve the affect of adding the title of the page to the url with the delimiter?

What is this technique called?

+5  A: 

This technique called 'URL Rewriting'. You tagged question with 'asp.net' so MSDN may help you about that: http://msdn.microsoft.com/en-us/library/ms972974.aspx

Ahmet Kakıcı
+6  A: 

The URL Rewriting for SO is provided by the Routing engine in ASP.NET MVC.

AndyMcKenna
+14  A: 

Use ASP.NET routing rather than rewriting when possible. It's available with both MVC and Web Forms. Routing is much more flexible and does a better job in passing context to the processing code, handling postbacks, etc.

You can also use the IIS7 Rewrite Module to handle rewriting at the webserver level before your ASP.NET code executes. There's some good information on how to do that here.

Jon Galloway
The ASP.NET routing wouldn't work with IIS6 by default, right? as far as I know it would require a work around: some kind of wild-card mapping, or url rewriting! +1 for a good answer though.
Galilyou
Both IIS5 and IIS6 must be configured with wildcard mappings for non-registered extensions (include "no extension")
Richard Szalay
+5  A: 

Stackoverflow is programmed in ASP.Net MVC and URL routing is a standard part of the package in MVC. Apart from URL routing it also offers many more advantages. So if you're building a new website, and want to get the benefit of URL Routing among other advantages, try making it in MVC.

Be warned though, you will have to learn quite a bit.

Cyril Gupta
+1  A: 

What is this technique called?

Like others have said the technique is called routing. Basically it takes your pretty formatted URL and maps it to some controller action. And according to Jon Galloway's answer IIS 7 has this functionality integrated. For previous versions of IIS you'll probably have to setup a wildcard application mapping to ASP.NET runtime and possibly add in your own HttpModule to your application's request pipeline to handle routing if your web framework of choice doesn't provide a routing facility.

How do you achieve the affect of adding the title of the page to the url with the delimiter?

You can accomplish this by lower casing the title and replacing non-alphanumeric characters with hyphens. Sometimes this bit is called a slug. You probably want to keep the slug length down as well so you don't run into URL length limit problems. You also have the option of generating the slug in a couple places:

  • When the title is submitted save the slug with the rest of the page data.
  • Or generate it on the fly when you generate pages that link to page with the title.

Keep in mind slugs should not be used to look up page data, that's what a page ID is for; the slug should be optional. Your routing rules will just be concerned with getting the ID out of the URL and giving it to the correct controller action meanwhile ignoring everything after. In other words the only crucial part is the question ID. The slug is just sugar. :)

Jonathon Watney
A: 

While routing is clearly the better option here, there are ways of faking it with minimal effort. For example, here's a simple way to get friendly URLs and some SEO:
Suppose you have the page:

example.aspx

Even without doing anything, the following URL will work:

example.aspx/some-friendly-text

You can also combine query data:

example.aspx?id=1
example.aspx/some-friendly-text?id=1

If you want, you can access that text using the request's PathInfo property.

Kobi
A: 

Don't forget that with this type of routing, people can link to your page with text you might have preferred them not to use.

I've seen this quite a lot with UK newspapers - they'll publish a story with a URL along the lines of

newspaperdoman.co.uk/articles/1128945/dog-bites-man

and then someone will link to it as

newspaperdoman.co.uk/articles/1128945/newspaper-in-crap-story-shocker

or whatever.

Vicky