我的网站使用了多个文件处理程序,并且它们都有重复的代码。为了整理我创建了一个共同的基类,他们都可以继承:
Public MustInherit Class HandlerBase
Implements System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Overridable Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
RequestHandler(context)
End Sub
Protected Overridable Sub RequestHandler(ByVal context As HttpContext)
Select Case context.Request.HttpMethod
Case "GET"
GetFile(context)
Case "POST"
UploadFile(context)
Case "DELETE"
DeleteFile(context)
Case Else
context.Response.ClearHeaders()
context.Response.StatusCode = 405
End Select
End Sub
Protected MustOverride Sub GetFile(ByVal context As HttpContext)
Protected MustOverride Sub UploadFile(ByVal context As HttpContext)
Protected MustOverride Sub DeleteFile(ByVal context As HttpContext)
end class
我的两个处理程序在继承基类之后工作得很好,但似乎根本没有。唯一的区别是这个处理程序覆盖了RequestHandler:
Protected Overrides Sub RequestHandler(ByVal context As HttpContext)
Select Case context.Request.HttpMethod
Case "GET"
GetFile(context)
Case Else
context.Response.ClearHeaders()
context.Response.StatusCode = 405
End Select
End Sub
我访问处理程序,并且从不在base和RequestHandler
中调用ProcessRequest答案 0 :(得分:0)
在fiddler检查请求后发现这是一个小错字:
Protected Overrides Sub DeleteFile(ByVal context As HttpContext
Throw New NotImplementedException()
End Sub
缺少参数的结束括号。我投票决定关闭这个。