站点根的通用处理程序

时间:2016-11-28 05:33:28

标签: asp.net vb.net iis-8.5

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

  • Windows Server 2012-R2 / IIS 8.5
  • 没有涉及MVC
  • 继承了ScriptModule-4.0模块,因此无扩展工作
  • 与2012年的this question类似,但从未得到妥善回答
  • 通用处理程序作为示例给出...也可以是Soap Web服务

我尝试了处理程序路径的各种斜杠和星号组合,但没有成功。

通用处理程序:

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>

1 个答案:

答案 0 :(得分:0)

我提出的解决方案,但我对其他想法持开放态度。

在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

如果网络应用位于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