how does stackoverflow put the question string as filename? [duplicate]
Possible Duplicate:
How does stackoverflow add the question title to the end of it's routes?
Hello all,
I'm inteding to develop a website somewhat similar to stackoverflow. I would also like that the question asked in my website will serve as the filename of the page. For example, stackoverflow question like "How does stackoverflow add the question title to the end of it's routes?" will result in this filename (as taken from the address bar): "Adding ID and title to URL slugs in ASP.NET MVChow-does-stackoverflow-add-the-question-title-to-the-end-of-its-routes"
How it is done in .开发者_StackOverflow中文版NET technology?
It's all about the technology you want to use.
In ASP.NET MVC world, you can use the built in Routing such as:
Route
routes.MapRoute(
"Question", // Route name
"questions/{id}/{name}", // URL with parameters
new {
controller = "Questions",
action = "Index",
id = "0",
name = UrlParameter.Optional
} // Parameter defaults
);
Controller
public ActionResult Index(int id, string name)
{
var model = _db.Questions.FirstOrDefault(x => x.Id == id);
return View(model);
}
Prior to .NET 3.5 Sp1, you could use Url Rewriting.
As Scott says, there was 2 main modules for this UrlRewriter.net and UrlRewriting.net
Though they were mainly use in ASP.NET 2.0, they work great in 3.5 as well.
Using ASP.Net MVC with ASP.Net Routing.
All of the other answers (MVC, ASP.NET Routing) but this can be done with ANY technology using URL rewriting. URL rewriting is supported by all web servers. Check out http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html for some apache how-to's
Edit: http://www.15seconds.com/Issue/030522.htm how to do url rewriting in .NET using httpmodules
StackOverflow uses ASP.NET MVC routes to accomplish this. The route would be something like this:
routes.MapRoute(
"Questions route", // Route name
"questions/{id}/{title}", // URL with parameters
new { controller = "Questions", action = "Question", id = "", title = "" } // Parameter defaults
);
Actually, what really matters to StackOverflow is the {id} part, since that is what is used to retrieve the question. The title is only used for Search Engine Optimization. You can try it yourself, just note that those three URLs give the same result:
how does stackoverflow put the question string as filename?
how does stackoverflow put the question string as filename?
how does stackoverflow put the question string as filename?
精彩评论