问题
I want to create a similar type of url as stack overflow does when a question is created.
Example:
Non-ajax GET/POST using jQuery (plugin?)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?
回答1:
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.
回答2:
The URL Rewriting for SO is provided by the Routing engine in ASP.NET MVC.
回答3:
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
回答4:
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.
回答5:
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. :)
回答6:
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.
回答7:
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.
来源:https://stackoverflow.com/questions/1151904/how-do-i-create-userfriendly-urls-like-stackoverflow