站点根的通用处理程序

Generic Handler for Site Root

我正在尝试让浏览器为站点根请求调用一个处理程序,即 http://my.example.com。鉴于下面的代码,如果我调用 /Test,处理程序将按预期工作,但如果没有它,我会得到 HTTP Error 403.14 - Forbidden(不允许目录浏览)。

我已经为处理程序路径尝试了各种斜线和星号组合,但都没有成功。

通用处理程序:

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 class,我在其中计算 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