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

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

问题描述

我刚开始添加新的.NET 4.0 URL重写到我的项目。我有一个问题。

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,这是很明显,不是很好看,也没有搜索引擎友好的链接。我想在链接显示而不是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?

非常感谢你!

推荐答案

没有什么可以从包括标识(用于快速SQL检索)和链接(搜索引擎优化的目的)文章的标题prevent你。这是exactelly如何计算器是处理路由(查询地址为这个问题)。

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友好string.Like使所有小写,将空格等字符的方法' - '等

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天全站免登陆