ddivita
Member
- Joined
- May 7, 2008
- Messages
- 19
- Programming Experience
- 10+
i have created an HTTP handler for mobil request. Basically, if a user is trying to access my non-mobi site with a wireless device I want them to be forwarded to my mobi site. The redirects seem to work fine, but when a user is coming to the site correctly the pages are not loading. Below is the line form the webconfig and the code for the handler:
<add verb="*" path="*.aspx" type="HTTPDomainHandler"/>
<add verb="*" path="*.aspx" type="HTTPDomainHandler"/>
VB.NET:
Imports System.Web
Imports System.Text
Imports System.Xml
Imports System.IO
Public Class HTTPDomainHandler
Implements IHttpHandler
Private Const MobiURL As String = "http://test.mobi"
Private Const httpUserAgentXmlFileName As String = "UserAgents.xml"
Private Const mobilUserAgentXpath As String = "/httpHandlers/mobil/userAgentRegEx"
Private _FilePath As String = String.Empty
#Region "Public Methods"
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
'Check Request for Mobil
RedirectToMobilSite(context)
End Sub
#End Region
#Region "Private Methods"
Private Sub RedirectToMobilSite(ByVal context As HttpContext)
'Check the Accept
Dim httpAccept As New RegularExpressions.Regex("/wap\.|\.wap/i")
If Not httpAccept.IsMatch(context.Request.ServerVariables("HTTP_ACCEPT")) Then
'Check the UserAgent
Dim userAgentXmlFile As New XmlDocument
userAgentXmlFile.Load(ParamPath & httpUserAgentXmlFileName)
If Not userAgentXmlFile Is Nothing Then
Dim userAgentNodes As XmlNodeList = userAgentXmlFile.SelectNodes(mobilUserAgentXpath)
If Not userAgentNodes Is Nothing AndAlso userAgentNodes.Count > 0 Then
For Each userAgentNode As XmlNode In userAgentNodes
Dim userAgentRegEx As New RegularExpressions.Regex("/" & userAgentNode.InnerText.ToString.Trim & "/i")
If userAgentRegEx.IsMatch(context.Request.ServerVariables("HTTP_USER_AGENT")) Then
context.Response.Redirect(MobiURL)
End If
Next
End If
End If
Else
context.Response.Redirect(MobiURL)
End If
End Sub
#End Region
#Region "Property Declarations"
Private ReadOnly Property ParamPath() As String
Get
If Not String.IsNullOrEmpty(_FilePath) Then
Return _FilePath
Else
Dim objFolder As New FileInfo(System.AppDomain.CurrentDomain.BaseDirectory)
_FilePath = objFolder.Directory.Root.Name & "area"
Return _FilePath
End If
End Get
End Property
#End Region
End Class