views:

653

answers:

5

I am using ASP.NET C#.

How do I implement URL re-writing procedure that is similar to StackOverflow.com?

http://stackoverflow.com/questions/358630/how-to-search-date-in-sql

Also, what is the meaning of values such as "358630" in the URL? Is this the question ID (the basis for which they use to fetch the data from the table)? Whatever it is, in my application I am identifying records using an "ID" field. This field is an identity column in an SQL table. Right now, my URLs are like the following:

http://myweb.com/showdetails.aspx?id=9872

But I'd like them to appear like:

http://myweb.com/showdetails/9872/my_question_title

Or:

http://myweb.com/9872/my_question_title

Or whatever the best way, which will taste good to search bots.

My application is hosted on Go Daddy's shared hosting service, and I feel that no customized ASP.NET "HTTP module" or no customized DLL for URL re-writing is working on their server. I tried many samples but no luck yet!

I found that Stack Overflow is hosted on Go Daddy (shared hosting?). Maybe Stack Overflow's method will work for me.

+11  A: 

SO is using ASP.NET MVC. You really need to read in details how MVC URL rewriting works, but the gist of it is that the 'questions' part in the URL is the name of the Controller class (which roughly corresponds to the 'showdetails' in your URL) and the number is a ID parameter for the default action on that Controller (same as the parameter 'id' in your URL).

Franci Penov
I am using ASP.NET 2.0 (C#) can I use ASP.NET MVC to develop my applications? Or I require ASP.NET 3.x+ to use ASP.NET MVC ?
Prashant
Prashant
Actually, you should check with your hosting service. Most companies that offer .Net 2.0 SP1 also usually offer .Net 3.5 as well, seeing how the CLR is the same and the latter only adds more classes to the framework.
Franci Penov
+2  A: 

Before the advent of System.Web.Routing, the common practice was to use UrlRewriter.NET. Worked well enough, but could bite you when configuring IIS. I'm not sure if there are any simple ways of using the new Routing classes in ASP.NET (i.e., drop it in and go vs. refactoring code).

Will
I can't use this because, UrlRewriter.NET requires installation in IIS, and as I am on shared hosting, so I don't have right to do so :((
Prashant
That's too bad... Some shared hosting providers offer it as an option; you might want to check with them.
Will
+3  A: 

Since MVC isn't an option you can try redirecting the 404s. This will work in ASP.NET 1.1 and above: Redirect 404s and 405s to your own handler using either IIS config or web.config, parse out the request in the handler and redirect to the appropriate resource.

<configuration>
   <system.web>
    <customErrors mode="On" defaultRedirect="error.html">
     <error statusCode="404" redirect="newHandler.aspx"/>
    </customErrors>       
   </system.web>
</configuration>
xcud
Man, that's a hackish way of doing it.... there's no side-effects from doing this?
Lloyd Cotten
Used to do this for smaller sites in classic ASP, egh.
Sciolist
Read the requirements. Tastes good to search engines, supported by ASP.NET 2.0, and works on a shared hosting. Do you have a less hackish" solution that fits the reqs?
xcud
I'm not sure this would taste good to search engines - wouldn't the IIS send back status 404?
Dominic Rodger
+2  A: 

please explain the meaning of values such as "358630" in the URL

That is (presumably) the ID for the question in the database. In the MVC model

 myurl.com/questions/358630

is analogous to

myurl.com/questions.aspx?id=358630

The question title on the end of the URL is actually being ignored by the app. It's generally "tacked on" for search engine optimization and human readability purposes. In fact, you can change the title of this question in the URL and notice the page still loads just fine.

Cory House
Yes, but with "ID" what I understand is if I'll increment +1 in this http://stackoverflow.com/questions/521310/how-is-stackoverflow-com-re-writing-urls url then it should open the next question withour changing other part of the url...
Prashant
But its forwarding me to this http://stackoverflow.com/questions/521298/when-to-use-struct-in-c/521311#521311 URL, how StackOverflow is maintaining this???
Prashant
I can only speculate, but perhaps they're redirecting to a more canonical answer in some instances. So for example, when you type in ID 521311, it redirects to 521298. So you can assume 521298 is the best answer on structs in C#.
Cory House
Perhaps questions and answers (and possibly comments) are all stored in the same table, with some discriminator column(s) to define the type/parent etc - I see that the answer that is referenced with a +1 to the id was left about the same time as your question.
Zhaph - Ben Duguid
It's fairly common in CMS's to store all content in one table, so I don't see why SO isn't doing the same.
Zhaph - Ben Duguid
+1  A: 

The new System.Web.Routing dll is part of ASP.NET 3.5 SP1, and is bin deployable on ASP.NET 3.5, so you could use the features of that on a classic ASP.NET WebForms site.

You'll probably want to take note of Phil Haack's comments in his post on using MVC on IIS 6 as you'll probably need to include the .aspx extension in your routed urls

http://www.mysite.com/controler.aspx/action/id

You might also want to check out Questions Tagged SEO.

The ignored question name at the end of the url is often called a "Slug", and is used for SEO purposes to include the page title in the url.

Zhaph - Ben Duguid