将 asp URL 映射到 aspx Http[Handler/Module]

发布于 2024-10-22 00:31:19 字数 1418 浏览 2 评论 0原文

我们正在为客户重写旧网站,从经典 ASPASP.NET 4。一旦上线,新网站将与旧网站位于同一根 URL 中。

但有一个规定是......旧站点过去使用诸如 article.asp?articleid=### 之类的页面来加载内容。新站点将使用类似的内容,但使用 ASP.NET 路由,例如 ~/articles/o/###(“o”代表“旧 id”)。

我需要能够捕获对旧 article.asp 页面的请求,并将 url 重写为新的 article.aspx url。

我最初的想法是在 Application_BeginRequest 中处理这个问题。 dll 处理,然后将以下代码添加到我的 global.asax 中的 Application_BeginRequest 中:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim ctx = HttpContext.Current, url = ctx.Request.RawUrl
    If Regex.IsMatch(url, "/article\.asp\?articleid=\d+") Then
        Dim oldid = Regex.Match(url, "(?:articleid=)(\d+)").Groups(1).Value
        Server.Transfer("~/articles/o/" & oldid)
    End If
End Sub

我将 .ASP 扩展名重新映射为由与 .ASPX 页面相同的处理程序 但不工作。因为现在,我需要在路径中包含一个实际的 article.asp ,否则我会收到 404 错误(即使如此,它实际上也不会执行任何操作) ,并且只显示一个空白页)。这可以正常工作(假设 ASP 页面中的重定向代码有效),但他们在自己的子目录中每个都有 500 个 article.asp 副本(破坏了 CMS 模型的全部要点)。他们希望保留旧的网址,以防有人点击电子邮件或书签中的旧网址。

我需要能够将此文件名映射到新路径,无论它位于哪个子目录,按照上面的 Application_BeginRequest 中的逻辑。执行此操作的正确方法是什么?由于 global.asax 不起作用,我假设我需要编写 HttpHandlerHttpModule 类,但我没有确定哪条路是正确的。

在 Windows Server 2003 R2 上运行 IIS。

谢谢!

We are rewriting an old site for a client, from Classic ASP to ASP.NET 4. The new site will be living at the same root URL as the old one once it is pushed live.

One stipulation though is this... the old site used to use a page such as article.asp?articleid=### to load content. The new site will be using something similar, but making use of ASP.NET Routing such as ~/articles/o/### (the 'o' is for 'old id').

I need to be able to catch requests to the old article.asp page and rewrite the url to the new article.aspx url.

My original thought was to handle this in the Application_BeginRequest. I remapped the .ASP extension to be handled by the same handler dll as .ASPX pages, and then added the following code to my Application_BeginRequest in my global.asax:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim ctx = HttpContext.Current, url = ctx.Request.RawUrl
    If Regex.IsMatch(url, "/article\.asp\?articleid=\d+") Then
        Dim oldid = Regex.Match(url, "(?:articleid=)(\d+)").Groups(1).Value
        Server.Transfer("~/articles/o/" & oldid)
    End If
End Sub

This did not work though. As this is right now, I need to have an actual article.asp in the path or else I get a 404 error (and even then it doesn't actually do anything, and just displays a blank page). This would work fine (assuming redirect code in the ASP page worked), except that they had 500 copies of article.asp each in their own sub-directory (breaking the whole point of a CMS model). They want to preserve the old urls in case someone clicks an old one from an email or bookmark.

I need to be able to map this file name to the new path, irregardless of the sub-directory it was in, as per the logic in my Application_BeginRequest above. What would be the proper way to do this? As global.asax isn't working, I'm assuming I need to either write an HttpHandler or HttpModule class, but I'm not sure which is the right way to go.

Running IIS on Windows Server 2003 R2.

Thanks!

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

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

发布评论

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

评论(1

飘逸的'云 2024-10-29 00:31:19

将上面的代码放在 global.asaxApplication_BeginRequest 方法中实际上是一个正确的位置。问题是 IIS 中又缺少一项设置。在将 .ASP 重新映射到 ASP.NET 处理程序的对话框中,您还需要确保关闭验证文件存在选项强>。这完成了该过程,我的旧页面现在映射到相应的新页面。

Putting my above code in the Application_BeginRequest method of global.asax actually WAS a right place. The problem was one more missing setting in IIS. In the dialog where you remap .ASP to the ASP.NET handler, you also need to make sure to turn off the option for Verify that file exists. This completed the procedure, and my old pages now map to the corresponding new pages.

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