2008-09-16 122 views
15

是否有排除使用HTTP模块的某些页面的好方法?排除使用HTTPModule的某些页面

我有一个应用程序使用自定义HTTP模块来验证会话。该HTTP模块设置像这样的web配置:

<system.web> 
    <!-- ... --> 
    <httpModules> 
    <add name="SessionValidationModule" 
     type="SessionValidationModule, SomeNamespace" /> 
    </httpModules> 
</system.web> 

从页面中排除的模块,我试着这样做(没有成功):

<location path="ToBeExcluded"> 
    <system.web> 
    <!-- ... --> 
    <httpModules> 
     <remove name="SessionValidationModule" /> 
    </httpModules> 
    </system.web> 
</location> 

有什么想法?

回答

11

您可以使用HTTPHandler而不是HTTPModule。处理程序允许您在Web.Config中声明它们时指定路径。

<add verb="*" path="/validate/*.aspx" type="Handler,Assembly"/> 

如果你必须使用一个HttpModule,你可以只检查请求的路径,如果它是一个被排除在外,绕过验证。

+0

我正在使用这种方法,它只是不工作。不知道这是如何被接受的答案。 – Kehlan 2014-05-29 20:38:52

+0

我尝试使用处理程序,但它似乎不适用作为处理程序重新路由http上下文。我问在这里:http://stackoverflow.com/questions/27124737/asp-net-httphandler-prevents-page-from-loading?noredirect=1#comment42750773_27124737 – user1531921 2014-11-25 12:12:50

13

HttpModules附加到ASP.NET请求处理管道本身。 httpModule本身必须考虑计算出它想要执行的请求以及它想忽略哪些请求。

这可以,例如,可以通过查看context.Request.Path属性来实现的。

5

下面是一些简单的例子,如何筛选通过延期的请求......下面的例子中,从与特定扩展名的文件处理排除。过滤按文件名看起来几乎与一些小的变化一样...

public class AuthenticationModule : IHttpModule 
{ 
    private static readonly List<string> extensionsToSkip = AuthenticationConfig.ExtensionsToSkip.Split('|').ToList(); 

    // In the Init function, register for HttpApplication 
    // events by adding your handlers. 
    public void Init(HttpApplication application) 
    { 
     application.BeginRequest += new EventHandler(this.Application_BeginRequest); 
     application.EndRequest += new EventHandler(this.Application_EndRequest); 
    } 

    private void Application_BeginRequest(Object source, EventArgs e) 
    { 
     // we don't have to process all requests... 
     if (extensionsToSkip.Contains(Path.GetExtension(HttpContext.Current.Request.Url.LocalPath))) 
      return; 

     Trace.WriteLine("Application_BeginRequest: " + HttpContext.Current.Request.Url.AbsoluteUri); 
    } 

    private void Application_EndRequest(Object source, EventArgs e) 
    { 
     // we don't have to process all requests... 
     if (extensionsToSkip.Contains(Path.GetExtension(HttpContext.Current.Request.Url.LocalPath))) 
      return; 

     Trace.WriteLine("Application_BeginRequest: " + HttpContext.Current.Request.Url.AbsoluteUri); 
    } 
} 

总体思路是在配置文件中究竟应该被处理(或从处理除外)指定和使用的配置参数中模块。