2013-09-30 40 views
1

我在Visual Studio 2012中的Cassini開發人員服務器上運行,並且我需要將客戶端從傳統.asp頁面重定向到.aspx頁面。如何將URL重定向/重寫.asp到.aspx

注:理想我想重定向從.asp客戶友好的URL,然後在內部做了重寫.aspx

POST /ResetClock.asp 

HTTP/1.1 307 Temporary Redirect 
Location: //stackoverflow.us/ResetClock 

,然後在內部:

POST /ResetClock 

重寫爲/ResetClock.ashx(是的,我將其更改爲.ashx;這是url重寫的優點)。

像什麼Hanselman在做

這是很多像what Scott Hanselman did

  • 申請/foo.html
  • 給客戶端重定向到/foo
  • 客戶端請求/foo
  • 是重 - 寫成/foo.html

嘗試的劈

我試圖破解溶液;改變.asp頁面強制重定向到.ashx(和生活的URL重寫語法改天打):

ResetClock.asp

<% 
    Response.Redirect("ResetClock.aspx") 
    Response.End 
%> 

除了卡西尼不提供.asp全部頁面:

這種類型的頁面沒有提供。

描述:您請求的頁面類型因爲已被明確禁止而無法投放。擴展名「.asp」可能不正確。請查看下面的網址,並確保它拼寫正確。

請求的URL: /WebSite/FetchTimes.asp

指向一個相關的問題。我最終使用的解決方案不能要求IIS7.5上尚未提供的任何內容。它不需要任何需要訪問IIS管理工具的東西;並且必須完全存在於網站內(例如web.config)。

問題

如何重新寫.asp到更多的東西ASP.net十歲上下?

編輯:改變GETPOST挫敗nitpickers誰不知道爲什麼307 Temporary Redirect,而不是302 Found303 See Other

+1

那麼,使用IIS或IIS Express有什麼問題? –

+0

@JohnSaunders使用IIS沒有錯。最後,網站將部署到IIS7.5機器上。不會飛的是必須以任何方式改變服務器的配置。 –

+2

我的意思是在你的開發機器上使用IIS Express。Visual Studio開發服務器(它是從代號爲「Cassini」的代碼開發的,對那些不瞭解古代歷史的人來說)根本就不是要做你想做的事情。 IIS Express _is_意味着要做你想做的事情。 –

回答

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頁

企業的首要任務是將客戶端重定向到一個「乾淨」形式。例如,對於一個/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頁面,ASP.net內容):

的web.config

<configuration> 
    <system.web> 
     <urlMappings> 
     <add url="~/ResetClock.asp" mappedUrl="~/ResetClock.ashx"/> 
     </urlMappings> 
    </system.web> 
</configuration> 

客戶端仍將似乎已經找到了一個資源在/ResetClock.asp,但答案的膽量將來自/ResetClock.ashx

注意:任何代碼發佈到公共領域。無需歸屬。