如何url重定向/重写.asp到.aspx

时间:2013-09-30 00:25:01

标签: asp.net asp-classic url-rewriting

我在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;这是网址重写的优点。)

和汉塞尔曼一样

这很像what Scott Hanselman did

  • 申请.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 Redirect302 Found的挑剔者

1 个答案:

答案 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),每次发出请求时都会运行。

如果他们要求ASP页面

,则发出客户端重定向

第一项业务是将客户端重定向到“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重定向

从一些未知版本的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

  

注意:任何代码都会发布到公共域中。无需归属。