2012-07-06 126 views
0

我有一個HttpHandler映射到aspnet_isapi.dll進行靜態文件在經典模式下使用IIS 7.5(.pdf文件)的自定義驗證檢驗:呼叫StaticFileHandler

void IHttpHandler.ProcessRequest(HttpContext context) 
{ 
    if(!User.IsMember) { 
    Response.Redirect("~/Login.aspx?m=1"); 
    } 
    else { 
    //serve static content 
    } 
} 

上面的代碼工作正常,除else語句邏輯。在else語句中,我只是想允許StaticFileHandler處理請求,但我無法對此進行排序。將不勝感激任何關於如何簡單地將文件「交還」回IIS以作爲正常的StaticFile請求提供請求的建議。

回答

4

直接回答你的問題,你可以創建一個StaticFileHandler並將其處理請求:

// Serve static content: 
Type type = typeof(HttpApplication).Assembly.GetType("System.Web.StaticFileHandler", true); 
IHttpHandler handler = (IHttpHandler)Activator.CreateInstance(type, true); 
handler.ProcessRequest(context); 

但是一個更好的想法可能是創建一個HTTP模塊,而不是HTTP處理程序:

public class AuthenticationModule : IHttpModule 
{ 
    public void Dispose() 
    { 
    } 

    public void Init(HttpApplication application) 
    { 
     application.AuthorizeRequest += this.Application_AuthorizeRequest; 
    } 

    private void Application_AuthorizeRequest(object sender, EventArgs e) 
    { 
     HttpContext context = ((HttpApplication)sender).Context; 
     if (!User.IsMember) 
      context.Response.Redirect("~/Login.aspx?m=1");  
    } 
} 
+0

如果我想以類似的方式提供.aspx頁面,而不是調用System.Web.StaticFileHandler類型,我可以使用相同的方法並調用某種類型:System.Web。這種類型是什麼? – 2012-08-19 16:17:48

+1

嘗試使用'BuildManager.CreateInstanceFromVirtualPath'方法。 – 2012-08-19 20:25:47