在ASP.NET类中访问响应对象

And*_*ers 6 asp.net cookies class httpresponse

我有一个函数检查cookie(按名称)是否存在:

Private Function cookieExists(ByVal cName As String) As Boolean
    For Each c As HttpCookie In Response.Cookies
        If c.Name = cName Then Return True
    Next
    Return False
End Function
Run Code Online (Sandbox Code Playgroud)

我有一个以特定于应用程序的方式处理cookie的类,我想将所有与cookie相关的函数合并到这个类中.但是,如果我只是将它从aspx页面(它当前所在的位置)移动到上述类,我就无法使用此代码,因为我收到了错误:'Name' Response is not declared. 我修改了类以允许传递Response对象的引用:

Public Function cookieExists(ByVal cName As String, ByRef Response As HttpResponse) As Boolean
    For Each c As HttpCookie In Response.Cookies
        If c.Name = cName Then Return True
    Next
    Return False
End Function
Run Code Online (Sandbox Code Playgroud)

我的问题是:有更好的方法吗?

Gor*_*ell 13

HttpContext.Current.Response
HttpContext.Current.Request
Run Code Online (Sandbox Code Playgroud)

  • 使用System.Web; (2认同)