站点根的通用处理程序

问题描述:

我试图让浏览器为站点根请求调用处理程序,即http://my.example.com。给定下面的代码,如果我打电话/测试,处理程序按预期工作,但没有,我得到HTTP Error 403.14 - Forbidden(目录浏览是不允许的)。站点根的通用处理程序

  • 的Windows Server 2012-R2/IIS 8.5
  • 没有MVC涉案
  • ScriptModule-4.0模块继承了这样扩展名的作品
  • 类似this question从2012年起,这是从来没有正确回答
  • 通用处理程序是作为一个例子...也可以是肥皂Web服务

我试过各种combinatio处理器路径的斜杠和星号不成功。

通用处理器:

Public Class Test 
    Implements IHttpHandler 

    Public Sub ProcessRequest(Context As HttpContext) _ 
     Implements IHttpHandler.ProcessRequest 

     With New StringBuilder 
      .AppendLine("<html>") 
      .AppendLine("<head>") 
      .AppendLine("<title>Test</title>") 
      .AppendLine("</head>") 
      .AppendLine("<body>") 
      .AppendLine("<p>Hello World</p>") 
      .AppendLine("</body>") 
      .AppendLine("</html>") 

      Context.Response.Write(.ToString) 
     End With 
    End Sub 
End Class 

...并在web.config中我有以下几点:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.web> 
     <compilation strict="false" explicit="true" debug="true" targetFramework="4.5.2" /> 
     <customErrors mode="Off" /> 
     <authentication mode="Windows" /> 
     <httpRuntime targetFramework="4.5.2" /> 
    </system.web> 

    <system.webServer> 
     <handlers> 
      <add verb="*" name="Test" type="MyApp.Test" path="Test" /> 
     </handlers> 

     <defaultDocument enabled="true"> 
      <files> 
       <clear /> 
       <add value="Test" /> 
      </files> 
     </defaultDocument> 
    </system.webServer> 
</configuration> 

我想出了解决方案,但我接受其他的想法。

在web.config中:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.web> 
     <compilation strict="false" explicit="true" debug="true" targetFramework="4.5.2" /> 
     <customErrors mode="Off" /> 
     <authentication mode="Windows" /> 
     <httpRuntime targetFramework="4.5.2" /> 

     <!-- Required for Web Services via Handlers --> 
     <webServices> 
      <protocols> 
       <add name="HttpGet" /> 
       <add name="HttpPost" /> 
      </protocols> 
     </webServices> 
    </system.web> 

    <system.webServer> 
     <handlers> 
      <add verb="GET,POST" name="Test" type="MyApp.Test" path="Test" /> 
     </handlers> 

     <modules> 
      <add name="AppModule" type="MyApp.AppModule" /> 
     </modules> 

     <defaultDocument enabled="false" /> 
     <directoryBrowse enabled="false" /> 
    </system.webServer> 
</configuration> 

,然后添加AppModule类中,在那里我评价HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath,做一个HttpContext.Current.RewritePath所以上面定义的处理器将它捡起来。

  • my.example.com
  • my.example.com/AnyFolder/ MyApplication的

匹配为 “〜/” 工作,如果Web应用程序是在站点根IIS或在网站内设置为应用程序:

Public Class AppModule 
    Implements IHttpModule 

    Friend WithEvents WebApp As HttpApplication 

    Public Sub Init(ByVal HttpApplication As HttpApplication) _ 
     Implements IHttpModule.Init 

     WebApp = HttpApplication 
    End Sub 

    Private Sub WebApp_BeginRequest(sender As Object, e As EventArgs) _ 
     Handles WebApp.BeginRequest 

     With HttpContext.Current 
      If .Request.AppRelativeCurrentExecutionFilePath = "~/" Then .RewritePath("~/Test") 
     End With 
    End Sub 

    Public Sub Dispose() _ 
     Implements IHttpModule.Dispose 

     Throw New NotImplementedException() 
    End Sub 
End Class