解決的辦法是創建一個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
。
注意:任何代碼發佈到公共領域。無需歸屬。
那麼,使用IIS或IIS Express有什麼問題? –
@JohnSaunders使用IIS沒有錯。最後,網站將部署到IIS7.5機器上。不會飛的是必須以任何方式改變服務器的配置。 –
我的意思是在你的開發機器上使用IIS Express。Visual Studio開發服務器(它是從代號爲「Cassini」的代碼開發的,對那些不瞭解古代歷史的人來說)根本就不是要做你想做的事情。 IIS Express _is_意味着要做你想做的事情。 –