2013-03-22 122 views
0

我想通過HttpModule處理靜態文件Web請求,以根據某些策略在CMS中顯示文檔。我可以過濾出一個請求,但我不知道如何直接處理這樣的請求,因爲asp.net應該這樣做。ASP.NET HttpModule請求處理

+0

ihttpmodule需要很多工作。標準頁面或mvc框架會更好嗎? – 2013-03-22 15:15:40

+0

我表達得很糟糕,我需要一個httpHandler或一個httpModule來處理靜態文件內容,並允許註冊用戶直接訪問文檔。 – duns 2013-03-22 16:03:55

回答

0

這是你在找什麼?假設你在集成管道模式下運行,所有請求都應該在這裏完成,所以如果未經授權,你可以終止該請求,否則就像平常一樣讓它通過。

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

    public void Init(HttpApplication context) 
    { 
     context.AuthorizeRequest += context_AuthorizeRequest; 
    } 

    void context_AuthorizeRequest(object sender, EventArgs e) 
    { 
     var app = (HttpApplication)sender; 

     // Whatever you want to test to see if they are allowed 
     // to access this file. I believe the `User` property is 
     // populated by this point. 
     if (app.Context.Request.QueryString["allow"] == "1") 
     { 
      return; 
     } 

     app.Context.Response.StatusCode = 401; 
     app.Context.Response.End(); 
    } 
} 

<configuration> 
    <system.web> 
    <httpModules> 
     <add name="CustomSecurityModule" type="MyModule1"/> 
    </httpModules> 
    </system.web> 
</configuration> 
+0

非常感謝您的回覆,我用Server.Transfer解決了這個問題。我不明白爲什麼返回指令不能提供請求的正確的靜態文件 – duns 2013-03-25 14:53:05