ASP.NET 友好 URL

发布于 2024-08-11 22:10:40 字数 558 浏览 12 评论 0原文

在我的研究中,我找到了两种方法来做到这一点。

两者都需要修改 Global.Asax 中的 Application_BeginRequest 过程,您可以在其中运行代码来执行实际的 URL 映射(我的数据库视图包含所有友好 URL 及其映射的“真实”URL)。现在的技巧是让您的请求通过 .NET 引擎运行,而无需使用 aspx 扩展名。我发现的两种方法是:

  1. 使用通配符应用程序扩展映射通过 .NET 引擎运行所有内容。

  2. 创建自定义 aspx 错误页面并告诉 IIS 向其发送 404。

现在我的问题是:

其中一个比另一个更好吗?

在我的开发服务器上玩时,我注意到的第一件事是它破坏了首页扩展,而不是这很重要,但这就是我习惯连接到我的网站的方式。我对 #1 的另一个问题是,尽管我的托管公司对我很宽容(因为我是他们最大的客户)并且会考虑做这样的事情,但他们对可能带来的任何安全风险保持警惕。

`#2 效果很好,但我只是有这种感觉,它不如 #1 有效。难道只是我的错觉吗?

谢谢

In my research, I found 2 ways to do them.

Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine was with a database view that contained all the friendly URLs and their mapped 'real' URLs). Now the trick is to get your requests run through the .NET engine without an aspx extension. The 2 ways I found are:

  1. Run everything through the .NET engine with a wildcard application extension mapping.

  2. Create a custom aspx error page and tell IIS to send 404's to it.

Now here's my question:

Is there any reason one of these are better to do than the other?

When playing around on my dev server, the first thing I noticed about #1 was it botched frontpage extensions, not a huge deal but that's how I'm used to connecting to my sites. Another issue I have with #1 is that even though my hosting company is lenient with me (as I'm their biggest client) and will consider doing things such as this, they are wary of any security risks it might present.

`#2 works great, but I just have this feeling it's not as efficient as #1. Am I just being delusional?

Thanks

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

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

发布评论

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

评论(4

秋凉 2024-08-18 22:10:40

我过去也用过#2。

它更高效,因为与通配符映射不同,ASP.NET 引擎不需要“处理”对所有附加资源(如图像文件、静态 HTML、CSS、Javascript 等)的请求。

或者,如果您不介意 .aspx 扩展名在您的网址中,您可以使用: http://myweb/app/idx.aspx/ products/1 - 效果很好。

尽管如此,真正的解决方案是使用 IIS 7,其中 ASP.NET 运行时是 IIS HTTP 模块堆栈的成熟部分。

I've used #2 in the past too.

It's more efficient because unlike the wildcard mapping, the ASP.NET engine doesn't need to 'process' requests for all the additional resources like image files, static HTML, CSS, Javascript etc.

Alternatively if you don't mind .aspx extension in your URL's you could use: http://myweb/app/idx.aspx/products/1 - that works fine.

Having said that, the real solution is using IIS 7, where the ASP.NET runtime is a fully fledged part of the IIS HTTP module stack.

锦爱 2024-08-18 22:10:40

如果您有最新版本的 IIS,则有重写模块 - 请参阅此处。如果没有,有免费的第三方二进制文件,您可以与旧版 IIS(即版本 6)一起使用 - 我使用过一个从 .ini 文件读取重写规则并支持正则表达式的二进制文件,但我不记得它的名字了,抱歉(它可能是 此)。我推荐这个,而不是 404 页面。

If you have the latest version of IIS there is rewrite module for it - see here. If not there are free third party binaries you can use with older IIS (i.e. version 6) - I have used one that reads the rewrite rules from an .ini file and supports regular expression but I cant remember its name sorry (its possibly this). I'd recommend this over cheaping it out with the 404 page.

锦上情书 2024-08-18 22:10:40

您必须通过 ASP.NET 引擎映射所有请求。 IIS 处理请求的方式是通过文件扩展名。默认情况下,它仅处理仅由 ASP.NET 处理的 .aspx、.ashx 等扩展名。原因是它增加了请求处理的开销。
我不久前写过如何使用 IIS 6 进行此操作,http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx

您从数据库进行映射是正确的。 RegEx 重写,就像在 MVC 中开箱即用的那样。这是因为它或多或少地迫使您将主键放在 URL 中,并且没有一个好的方法来映射 URL 中不允许的字符,例如 '.

You have to map all requests through the ASP.NET engine. The way IIS processes requests is by the file extension. By default it only processes the .aspx, .ashx, etc extensions that are meant to only be processed by ASP.NET. The reason is it adds overhead to the processing of the request.
I wrote how to do it with IIS 6 a while back, http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx.

You are right in doing your mapping from the database. RegEx rewriting, like is used out of the box in MVC. This is because it more or less forces you to put the primary key in the URL and does not have a good way to map characters that are not allowed in URLs, like '.

智商已欠费 2024-08-18 22:10:40

您是否检查了 ASP .Net MVC 框架?使用该框架,您的所有 URL 都会自动映射到控制器,控制器可以执行任何所需的操作(包括重定向到其他 URL 或控制器)。您还可以使用自定义参数设置自定义路由。如果您还没有看过,也许值得一看。

Did you checked the ASP .Net MVC Framework? Using that framework all your URLs are automatically mapped to Controllers which could perform any desired action (including redirecting to other URLs or controllers). You could also set custom routes with custom parameters. If you don't have seen it yet, maybe it will worth the look.

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