ASP.NET 4.0 URL 重写:如何处理 ID [英] ASP.NET 4.0 URL Rewriting: How to deal with the IDs

查看:21
本文介绍了ASP.NET 4.0 URL 重写:如何处理 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始将新的 .NET 4.0 URL Rewriting 添加到我的项目中.我有一个问题.

I have just started adding the new .NET 4.0 URL Rewriting into my project. I have a question.

假设我有一个显示文章的 Article.aspx.我在 Global.asax 中为它做了一条路线:

Let's say I have a Article.aspx that displays, well, articles. I made a route for it in the Global.asax:

routes.MapPageRoute("article-browse", "article/{id}", "~/Article.aspx");

因此链接由文章的 id 组成,这显然不是一个很好的链接,也不是 SEO 友好的链接.我想在链接中显示文章的标题,而不是 ID.

So the link consists of the article's id which is, obviously, not a very nice, nor SEO friendly link. I would like to display the Article's title in the link, instead of the ID.

我是否必须在参数中传递整个标题(而不是 id),然后进行 SQL 查询以搜索具有匹配标题的数据库记录?这听起来很可怕.也许有一些方法可以做一些类似于 Eval() 方法的事情,将标题更改为 ID?

Do I have to pass the whole title in the parameter (instead of the id) and then make a SQL query that searches for a database record with the matching title? That sounds scary. Maybe there is some way to do something similar to the Eval() methods, that would change the title into an ID?

非常感谢!

推荐答案

没有什么可以阻止您在链接中包含 ID(用于快速 SQL 检索)和文章标题(用于 SEO 目的).这正是 stackoverflow 处理路由的方式(检查此问题的地址).

There is nothing to prevent you from including both the ID (for quick SQL retrieval) and the article's title in the link (for SEO purposes). This is exactelly how stackoverflow is handling the routing (check the address for this question).

routes.MapPageRoute("article-browse", "article/{id}/{title}", "~/Article.aspx");

显然,ID后面的标题不是显示页面所必需的(你只用ID来获取文章),但是每次你在你的网站中生成链接时,用标题生成它,机器人会使用为您的网页编制索引时.

Obviously, the title after the ID is not necessary to display the page (you only use the ID to fetch the article), but everytime you generate the link in your site, generate it with the title, and the bots will use that when indexing your pages.

哦,您可能还想创建一种方法,将您的标题转换为 URL 友好的字符串.例如全部小写、将空格和其他字符转换为-"等.

Oh, and you might also want to create a method that translates your title into a URL-friendly string.Like making all lowercase, converting spaces and other characters to '-',etc.

这篇关于ASP.NET 4.0 URL 重写:如何处理 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆