22

On some .NET driven sites URLs don't end with asp.net page names, like default.aspx, instead they use a pattern http://sitename.com or http://sitename.com/subdirectory/subdirectory. The site is mapped as sub directories off the root, ie. /tags, /users, /badges, the URLs would be /tags, /users, /badges respectively.

Stack Overflow, to use a specific example, uses question URLs of the form How do get clean URLs like Stackoverflow?. Which is great way to optimize the page for search engines.

Is this implemented using HTTP handlers? Is the GET request filtered based on path and the whole response is formed in the handler itself based on the question id? Anyone else care to speculate?

26
  • 4
    This question has been asked so many times before. Jun 23, 2009 at 21:11
  • 5
    Drop the SO references and this is a great question. Jun 23, 2009 at 21:12
  • 9
    Wow; a legitimate question gets down-voted three times, marked falsely as "duplicate" to a question about ROR, called "not a real question" even though it is - and my reference to url_rewriting is down-voted. This is a complete fiasco! :)
    – Sampson
    Jun 23, 2009 at 21:16
  • 16
    Essentially the guy wants to know how the routing works, and is using stackoverflow as a frame of reference. Give him a break.
    – Doug R
    Jun 23, 2009 at 21:19
  • 8
    I think the pedants should go be pedantic elsewhere. There's nothing wrong with using SO as a reference to the question.
    – Jeff Yates
    Jun 23, 2009 at 21:39

5 Answers 5

23

It's ASP.Net MVC, which has the .Net Routing more or less built in. The Routing is available for non MVC projects as well, though

http://msdn.microsoft.com/en-us/library/cc668201.aspx

It's just a .dll you can drop in your bin folder. Basically it uses regular expressions to match your URL's to pages/templates.

1
18

This is achieved with mod_rewrite in Apache, or similar methods of url_rewriting on IIS.

Note: SOFlow uses the latter.

4
  • Probably because it's well documented that the site doesn't use Apache. Jun 23, 2009 at 21:16
  • 7
    Did I say it uses Apache? No. The asker was interested in how the URL's work. He didn't specify that he was interested in how they work from a .NET perspective. I offered an answer for both Apache and IIS.
    – Sampson
    Jun 23, 2009 at 21:17
  • @Jonathan Sampson the title does indicate/imply that he is asking about how SO does it but it isn't explicitly stated in the question itself. +1 for your answer still being correct regardless.
    – Bryan
    Jun 23, 2009 at 21:28
  • 5
    @Bryan, I agree that he's using SO as the example, but we shouldn't assume that he must therefore be expecting only the technologies that SO uses (since he obviously don't know what they are to begin with). I am a PHP developer, and yet I might ask "how did SO accomplish x..." so that I can emulate it within my own sphere of technologies. I just want to make sure I'm not assuming too much when it comes to answering a question :) Thanks for the ++;
    – Sampson
    Jun 23, 2009 at 21:37
5

The URLs are in that format following REST principles in which everything is a resource with an unique URL.

I think I read somewhere in the blog that this is achieved by using ASP.NET MVC framework.

3

I know that Stack Overflow is using the ASP.NET MVC framework, which presumably has a URL rewriting system built-in. For non-Windows system, Apache mod_rewrite is very common.

For example, a wiki page: http://server.com/wiki/Main_Page request is handled by the webserver. It's translated into /wiki/index.php?page=Main_Page

Here's an example of URL rewriting in Apache:

RewriteEngine on
RewriteRule ^forum-([0-9]+)\.html$ forumdisplay.php?fid=$1 [L,QSA]
RewriteRule ^forum-([0-9]+)-page-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2 [L,QSA]

RewriteRule ^thread-([0-9]+)\.html$ showthread.php?tid=$1 [L,QSA]
RewriteRule ^thread-([0-9]+)-page-([0-9]+)\.html$ showthread.php?tid=$1&page=$2 [L,QSA]

What this says is if the URL that comes in is forum-##.html then process that request as if it were forumdisplay.php?fid=##. The same goes for the thread-##.html rules.

0

You can do it in ASP.net by using Context.RewritePath.

In Global.asax, create an Application.BeginRequest event handler.

For example, if you want to make requests of

example.com/questions

actually return the results from

example.com/Questions/Default.aspx

Global.asax:

<%@ Application Language="C#" %>
<script runat="server">

   void Application_BeginRequest(Object sender, EventArgs e)
   {
       string originalPath = HttpContext.Current.Request.Path.ToLower();

       if (originalPath.Contains("/questions"))
       {
           String newPath = originalPath.Replace("/questions", "/Questions/Questions.aspx");
           Context.RewritePath(newPath);
       }
    }
</script>

If your web-site is running anything before .NET Framework 4, you will have to manually turn on the runAllManagedModulesForAllRequests in web.config, otherwise the BeginRequest event will not get fired:

<configuration>
...
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
   </system.webServer>
</configuration>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.