将 asp URL 映射到 aspx Http[Handler/Module]
我们正在为客户重写旧网站,从经典 ASP 到ASP.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
不起作用,我假设我需要编写 HttpHandler
或 HttpModule
类,但我没有确定哪条路是正确的。
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将上面的代码放在
global.asax
的Application_BeginRequest
方法中实际上是一个正确的位置。问题是 IIS 中又缺少一项设置。在将.ASP
重新映射到ASP.NET
处理程序的对话框中,您还需要确保关闭验证文件存在选项强>。这完成了该过程,我的旧页面现在映射到相应的新页面。Putting my above code in the
Application_BeginRequest
method ofglobal.asax
actually WAS a right place. The problem was one more missing setting in IIS. In the dialog where you remap.ASP
to theASP.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.