如何创建像 stackoverflow 这样用户友好的 url?

发布于 2024-07-27 21:01:35 字数 273 浏览 6 评论 0原文

我想创建一个与创建问题时堆栈溢出类似的 url 类型。

示例:

使用 jQuery(插件?)的非 ajax GET/POSTnon-ajax-get-post-using- jquery-plugin

我对最后一部分特别感兴趣,我用粗体突出显示了它。 如何实现将页面标题添加到带有分隔符的url中的效果?

这种技术叫什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

隐诗 2024-08-03 21:01:35

尽可能使用 ASP.NET 路由,而不是重写。 它可用于 MVC 和 Web 表单。 路由更加灵活,并且在将上下文传递给处理代码、处理回发等方面做得更好。

您还可以使用 IIS7 重写模块,用于在 ASP.NET 代码执行之前处理 Web 服务器级别的重写。 有一些关于如何执行此操作的好信息 在这里

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.

迷离° 2024-08-03 21:01:35

SO 的 URL 重写由 ASP.NET MVC 中的路由引擎提供。

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

瀟灑尐姊 2024-08-03 21:01:35

这种技术称为“URL 重写”。 您用“asp.net”标记了问题,因此 MSDN 可能会帮助您解决此问题:
http://msdn.microsoft.com/en-us/library/ms972974。 ASPX

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

滥情空心 2024-08-03 21:01:35

Stackoverflow 使用 ASP.Net MVC 进行编程,URL 路由是 MVC 中包的标准部分。 除了 URL 路由之外,它还提供了更多优点。 因此,如果您正在构建一个新网站,并且希望获得 URL 路由以及其他优势,请尝试使用 MVC 进行构建。

但请注意,您将需要学习很多东西。

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.

坐在坟头思考人生 2024-08-03 21:01:35

这种技术叫什么?

正如其他人所说,该技术称为路由。 基本上,它会采用格式漂亮的 URL 并将其映射到某些控制器操作。 根据 Jon Galloway 的答案 IIS 7集成了此功能。 对于以前版本的 IIS,如果您选择的 Web 框架不提供路由功能,您可能需要设置一个映射到 ASP.NET 运行时的通配符应用程序,并且可能需要将您自己的 HttpModule 添加到应用程序的请求管道中以处理路由。

如何达到这样的效果
将页面标题添加到
带分隔符的 url?

您可以通过小写标题并用连字符替换非字母数字字符来实现此目的。 有时这部分被称为 slug。 您可能还想保持长度较短,这样就不会遇到 网址长度限制问题。 您还可以选择在几个位置生成 slug:

  • 提交标题时,将 slug 与页面数据的其余部分一起保存。
  • 或者当您生成链接到带有标题的页面的页面时动态生成它。

请记住,slugs 不应该用于查找页面数据,这就是页面 ID 的用途; slug 应该是可选的。 您的路由规则将只关心从 URL 中获取 ID 并将其提供给正确的控制器操作,同时忽略之后的所有内容。 换句话说,唯一关键的部分是问题 ID。 蛞蝓只是糖。 :)

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

太阳哥哥 2024-08-03 21:01:35

虽然路由显然是更好的选择,但有一些方法可以轻松伪造它。 例如,以下是获取友好 URL 和一些 SEO 的简单方法:
假设您有以下页面:

example.aspx

即使不执行任何操作,以下 URL 也将工作:

example.aspx/some-friendly-text

您还可以组合查询数据:

example.aspx?id=1
example.aspx/some-friendly-text?id=1

如果需要,您可以使用请求的 PathInfo 属性

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.

杀手六號 2024-08-03 21:01:35

不要忘记,通过这种类型的路由,人们可以使用您可能不希望他们使用的文本链接到您的页面。

我在英国报纸上见过很多这样的情况——他们会发布一个故事,其URL类似于

newspaperdoman.co.uk/articles/1128945/dog-bites-man

,然后有人会以

newspaperdoman的形式链接到它。 co.uk/articles/1128945/newspaper-in-crap-story-shocker

或其他。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文