我在Visual Studio 2012中运行Cassini开发人员服务器,我需要将客户端从旧版.asp
页重定向到.aspx
页。
注意:理想情况下,我会将客户端从.asp
重定向到友好网址,然后在内部执行重写到{ {1}}
.aspx
然后在内部:
POST /ResetClock.asp
HTTP/1.1 307 Temporary Redirect
Location: //stackoverflow.us/ResetClock
重写为POST /ResetClock
(是的,我将其更改为/ResetClock.ashx
;这是网址重写的优点。)
.ashx
/foo.html
/foo
/foo
我试过黑客解决方案;更改/foo.html
页面以强制重定向到.asp
(并且在另一天使用url重写语法进行实战):
ResetClock.asp
.ashx
除了卡西尼根本不提供<%
Response.Redirect("ResetClock.aspx")
Response.End
%>
页面之外:
不提供此类型的页面。
说明:您已请求的页面类型未被提供,因为它已被明确禁止。扩展名“.asp”可能不正确。请查看下面的网址,确保拼写正确。
请求的网址: /WebSite/FetchTimes.asp
哪个指向相关问题。我最终使用的解决方案不需要IIS7.5上尚未提供的任何内容。并且它不需要任何需要访问IIS Admin工具的东西;并且必须完全存在于网站内(例如.asp
)。
如何将web.config
重写为更多ASP.net-ish?
修改:将.asp
更改为GET
以阻止那些想知道为什么POST
而非307 Temporary Redirect
或302 Found
的挑剔者
答案 0 :(得分:3)
解决方案是创建IHttpModule
。 HttpModules允许您拦截每个请求,并根据需要做出反应。
第一步是创建IHttpModule
:
class UrlRewriting : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(this.Application_BeginRequest);
application.EndRequest += new EventHandler(this.Application_EndRequest);
}
public void Dispose()
{
//Nothing to do here
}
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
}
private void Application_EndRequest(object sender, EventArgs e)
{
}
}
然后在web.config
文件中注册我们的 HttpHandler :
<强>的web.config 强>:
<configuration>
<system.web>
<httpModules>
<add name="UrlRewriting" type="UrlRewriting"/>
</httpModules>
</system.web>
</configuration>
现在我们有一个方法(Application_BeginRequest
),每次发出请求时都会运行。
第一项业务是将客户端重定向到“clean”表单。例如,/File.asp
的请求被重定向到/File
:
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//Redirct any requests to /File.asp into a /File
if (context.Request.Url.LocalPath == VirtualPathUtility.ToAbsolute("~/File.asp"))
{
//Be sure to issue a 307 Temporary Redirect in case the client issued a POST (i.e. a non-GET)
//If we issued 302 Found, a buggy client (e.g. Chrome, IE, Firefox) might convert the POST to a GET.
//If we issued 303 See Other, the client is required to convert a POST to a GET.
//If we issued 307 Temporary Redirect, the client is required to keep the POST method
context.Response.StatusCode = (int)HttpStatusCode.TemporaryRedirect;
context.Response.RedirectLocation = VirtualPathUtility.ToAbsolute("~/File");
context.Response.End();
}
}
现在客户端询问 /File
,我们必须在内部重新编写.aspx
,或者在我的情况下,.ashx
文件:
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//Redirct any requests to /ResetClock.asp into a /File
if (context.Request.Url.LocalPath == VirtualPathUtility.ToAbsolute("~/ResetClock.asp"))
{
//Be sure to issue a 307 Temporary Redirect in case the client issued a POST (i.e. a non-GET)
//If we issued 302 Found, the buggy client might convert the POST to a GET.
//If we issued 303 See Other, the client is required to convert a POST to a GET.
//If we issued 307 Temporary Redirect, the client is required to keep the POST method
context.Response.StatusCode = (int)HttpStatusCode.TemporaryRedirect;
context.Response.RedirectLocation = VirtualPathUtility.ToAbsolute("~/ResetClock");
context.Response.End();
}
//Rewrite clean url into actual handler
if (context.Request.Url.LocalPath == VirtualPathUtility.ToAbsolute("~/ResetClock"))
{
String path = "~/ResetClock.ashx"; //no need to map the path
context.Server.Execute(path, true);
//The other page has been executed
//Do not continue or we will hit the 404 of /ResetClock not being found
context.Response.End();
}
}
从一些未知版本的IIS开始,他们添加了一个(现在是模拟的)URL重写形式。它不发布客户端重定向,只发布内部重写。但至少它可以用来解决我的问题(响应ASP.net内容的ASP页面):
<强>的web.config 强>
<configuration>
<system.web>
<urlMappings>
<add url="~/ResetClock.asp" mappedUrl="~/ResetClock.ashx"/>
</urlMappings>
</system.web>
</configuration>
客户端似乎仍然在/ResetClock.asp
找到了资源,但响应的内容来自/ResetClock.ashx
。
注意:任何代码都会发布到公共域中。无需归属。