问题描述
我们正在用 .NET 3.5 解决方案替换旧的经典 asp 网站.
We are replacing an old classic asp website with a .NET 3.5 solution.
我们需要将所有经典 ASP 请求重定向到 aspx 页面(即 contactus.asp,现在可能会路由到/contact-us/default.aspx).我想要的是请求访问 global.asax,这样我就可以做类似
We need to redirect all of the classic ASP requests to aspx pages (i.e. contactus.asp, may now route to /contact-us/default.aspx). What I woudl like is for the requests to hit global.asax so I can do something like
If url == "bob.asp"
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", SiteConfig.SiteURL + redirectUrl);
End If
有两种不优雅的解决方案.
There are two inelegant solutions.
A) 放置一个 global.asa 文件并通过该文件进行路由.
A) Place a global.asa file and do the routing through that.
B) 将 asp 文件映射到 .NET 引擎.很好,但是如果我们需要在我们的站点上托管经典的 asp 站点,IIS 会将请求发送到错误的位置.
B) Map asp files to the .NET engine. Great, but then if we need to host classic asp sites on our sites IIS will be sending the requests to the wrong place.
我在这里找到了一个不错的解决方案
I found a nice solution here
http://forums.asp.net/p/1202225/3458901.aspx
这表明这样的事情可能会起作用......
Which stated something like this may work...
<buildProviders>
<add extension=".php" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
<httpHandlers>
<add verb="*" path="*.php" type="System.Web.UI.PageHandlerFactory" validate="True" />
</httpHandlers>
此示例适用于 php,但我认为同样适用于 asp.但是,在示例中将 .php 更改为 .asp 并将标签放置在 web.config 的正确部分之后,我没有任何乐趣(实际上是 500 服务器错误).
This example was for php but I assume the same thing would work for asp. However after changing .php to .asp in the example and placing the tags in the correct part of the web.config I'm having no joy (a 500 server error actually).
任何人都可以对此有所了解或给我一个优雅的解决方案.
Can anyone shed any light on this or give me an elegant solution.
感觉上述解决方案不适用于 php 或 asp,因为 IIS 将在请求到达 .NET 引擎之前对其进行路由.
Had a feeling the above solution wouldnt work for php or asp as IIS will have routed the request before it gets to the .NET engine.
提前致谢
史蒂夫
推荐答案
大@EdSF 在评论中指出我的答案是错误的.我难以置信地使用 Firebug 进行了检查,事实上,这是错误的.
Big edit: I was pointed by @EdSF in the comments that the answer was wrong. In disbelief I checked using Firebug, and, in fact, it was wrong.
您需要使用 Context.Response.RedirectLocation
才能使状态码起作用.
You need to use Context.Response.RedirectLocation
for the status code to work.
我在 global.asax 中也是这样做的:
I'm doing the same in global.asax:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
Dim fullOriginalpath As String = Request.Url.ToString.ToLower
If (fullOriginalpath.Contains("/verarticulo.asp?articuloid=")) Then
Context.Response.StatusCode = 301
''// this does not work, returns a 302
''//Context.Response.Redirect("/noticias/" + getIDFromPath(fullOriginalpath))
''// this does right way
Context.Response.RedirectLocation = "/noticias/" + getIDFromPath(fullOriginalpath)
Context.Response.End()
ElseIf (fullOriginalpath.Contains("/archivo.asp")) Then
Context.Response.StatusCode = 301
Context.Response.RedirectLocation = "/archivo/"
Context.Response.End()
ElseIf (fullOriginalpath.EndsWith("/default.asp")) Then
Context.Response.StatusCode = 301
Context.Response.RedirectLocation = "/"
Context.Response.End()
End If
End Sub
如果您使用 II6,您唯一需要做的就是配置此 ISAPI 过滤器这样:
The only thing you have to do if you are using II6 you have to configure this ISAPI filter in this way:
文件是c:windowsmicrosoft.netframeworkv2.0.50727aspnet_isapi.dll
这篇关于将经典 ASP 请求路由到 .NET - SEO 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!