Public
Sub Init(ByVal context
As System.Web.HttpApplication)
Implements System.Web.IHttpModule.Init
AddHandler context.BeginRequest,
AddressOf OnBeginRequest
End Sub
Private Sub OnBeginRequest(ByVal sender
As Object,
ByVal e As EventArgs)
Dim app As HttpApplication
app = CType(sender, HttpApplication)
If app.Request.RawUrl.ToLower().Contains("/keyvan") Then
app.Context.RewritePath("profile.aspx", "", "uid=1")
End If
End Sub
End
Class
but when i try to replace them with values from database it does not work.what can the problem be?
Private Sub OnBeginRequest(ByVal sender
As Object,
ByVal e As EventArgs)
Dim strSQl
As String
Dim dr
As SqlDataReader
Dim cmd As
New SqlCommand
Dim Conn As
New SqlConnection
strSQl =
"SELECT userid,username from users"
Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)cmd
= New SqlCommand(strSQl, Conn)
Conn.Open()
dr = cmd.ExecuteReader()
'dr.Read()
Dim app
As HttpApplication
app =
CType(sender, HttpApplication)
Do While (dr.Read())
If app.Request.RawUrl.ToLower().Contains("/" & dr("username") &
"") Then
app.Context.RewritePath("profile.aspx",
"", "uid=""" & dr("userid") &
"""")
'Else
' Current.Response.Redirect("err.index")
End If
Loop
It is not clear what the problem is from your post.
Is it giving you an error or just not redirecting ? If it giving you an error please provide the details about the error. If it is not doing the rewrite (not exectuing the line: app.Context.RewritePath("profile.aspx",
"", "uid=""" & dr("userid") &
""""), in this case there can be multiple posibilities.
1. You do not have the user that you are looking for ih the Users table.
2. The user name is not the same (has uppercase letters for example or spaces in the name).
3. Or maybe the users table is empty.
So you may want to rewrite the code to look somthing like this:
dim userName as string
' check if hte value dbnull
if (System.Convert.IsDBNull(dr("username"))) then
userName = ""
else
userName = dr("username").ToString()
end if
userName = userName.ToLower().Trim()
If app.Request.RawUrl.ToLower().Contains("/" & userName &
"") Then
app.Context.RewritePath("profile.aspx",
"", "uid=""" & dr("userid") &
"""")
it is not redirecting i am using the right usernames
i even added your code whenever i use a username it redirects to index.aspx instead of profile.aspx?uid=
it works when i use simple code without dr
it's interesting if i use else like this the application somehow hangs?
If app.Request.RawUrl.ToLower().Contains("/" & userName &
"") Then
app.Context.RewritePath("profile.aspx",
"", "uid=""" & dr("userid") &
"""")
Else
Current.Response.Redirect("err.aspx")
End If
keyvan1
Member
85 Points
270 Posts
urlrewrite problem
Sep 20, 2007 10:19 AM|LINK
i have this urlrewrite class which works fine
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init AddHandler context.BeginRequest, AddressOf OnBeginRequest End Sub Private Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)Dim app As HttpApplication
End Subapp = CType(sender, HttpApplication)
If app.Request.RawUrl.ToLower().Contains("/keyvan") Then
app.Context.RewritePath("profile.aspx", "", "uid=1")
End If
End
Classbut when i try to replace them with values from database it does not work.what can the problem be?
Private Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs) Dim strSQl As String Dim dr As SqlDataReader Dim cmd As New SqlCommand Dim Conn As New SqlConnectionstrSQl =
"SELECT userid,username from users" Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)cmd = New SqlCommand(strSQl, Conn)Conn.Open()
dr = cmd.ExecuteReader()
'dr.Read() Dim app As HttpApplicationapp =
CType(sender, HttpApplication) Do While (dr.Read()) If app.Request.RawUrl.ToLower().Contains("/" & dr("username") & "") Then app.Context.RewritePath("profile.aspx", "", "uid=""" & dr("userid") & """") 'Else ' Current.Response.Redirect("err.index") End If Loopgonzo11
Participant
778 Points
124 Posts
Re: urlrewrite problem
Sep 20, 2007 12:38 PM|LINK
Hello keyvan1,
It is not clear what the problem is from your post.
Is it giving you an error or just not redirecting ? If it giving you an error please provide the details about the error. If it is not doing the rewrite (not exectuing the line: app.Context.RewritePath("profile.aspx", "", "uid=""" & dr("userid") & """"), in this case there can be multiple posibilities.
1. You do not have the user that you are looking for ih the Users table.
2. The user name is not the same (has uppercase letters for example or spaces in the name).
3. Or maybe the users table is empty.
So you may want to rewrite the code to look somthing like this:
dim userName as string
' check if hte value dbnull
if (System.Convert.IsDBNull(dr("username"))) then
userName = ""
else
userName = dr("username").ToString()
end if
userName = userName.ToLower().Trim()
If app.Request.RawUrl.ToLower().Contains("/" & userName & "") Then app.Context.RewritePath("profile.aspx", "", "uid=""" & dr("userid") & """")hope this helps
keyvan1
Member
85 Points
270 Posts
Re: urlrewrite problem
Sep 20, 2007 01:50 PM|LINK
it is not redirecting i am using the right usernames
i even added your code whenever i use a username it redirects to index.aspx instead of profile.aspx?uid=
it works when i use simple code without dr
it's interesting if i use else like this the application somehow hangs?
If app.Request.RawUrl.ToLower().Contains("/" & userName & "") Then app.Context.RewritePath("profile.aspx", "", "uid=""" & dr("userid") & """") Else Current.Response.Redirect("err.aspx") End Ifisn't the problem with this line:????
"uid=""" & dr("userid") & """")
Loopkeyvan1
Member
85 Points
270 Posts
Re: urlrewrite problem
Sep 20, 2007 01:55 PM|LINK
sth important i noticed
if i write a username that does not exists i get error:
The resource cannot be found
but when i use right usernames i get redirected to index.aspx
i am sure the problem is with this line:app.Context.RewritePath(
"profile.aspx", "", "uid=""" & dr("userid") & """")keyvan1
Member
85 Points
270 Posts
Re: urlrewrite problem
Sep 20, 2007 02:03 PM|LINK
finally this worked
Dim userid As String = "uid=" & dr("userid") 'app.Context.RewritePath("profile.aspx", "", "uid=""" & dr("userid") & """")right: app.Context.RewritePath(
"profile.aspx", "", userid)why didn't that line with quotations work but the next one with userid worked?