How do I create userfriendly urls like stackoverfl

2020-06-17 05:46发布

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?

7条回答
forever°为你锁心
2楼-- · 2020-06-17 06:10

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

查看更多
We Are One
3楼-- · 2020-06-17 06:13

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

查看更多
霸刀☆藐视天下
4楼-- · 2020-06-17 06:19

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楼-- · 2020-06-17 06:22

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. :)

查看更多
一纸荒年 Trace。
6楼-- · 2020-06-17 06:24

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.

查看更多
欢心
7楼-- · 2020-06-17 06:25

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.

查看更多
登录 后发表回答