Keep in touch

you can keep in touch with my all blogs and sites by install this Toolbar...
http://rworldtoolbar.ourtoolbar.com/

Saturday, March 6, 2010

URL Rewritting: Hide Web Page Extentsion

write this code in ur Global.asax

Private Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim fullOrigionalpath As String = Request.Url.ToString()
If Not fullOrigionalpath.EndsWith(".aspx") Then
Context.RewritePath(fullOrigionalpath.Substring(fullOrigionalpath.IndexOf("/", 9)) & ".aspx")
End If
End Sub

Check if Url Exists or not in ASP.NET

Public Shared Function IsValidUrl(ByVal Url As String) As Boolean
Dim sStream As Stream
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse
Try
URLReq = WebRequest.Create(Url)
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New StreamReader(sStream).ReadToEnd()
Return True
Catch ex As Exception
‘Url not valid
Return False
End Try
End Function

Convert A Number To Month Name and Month Name to Number in ASP.NET

For Converting A Number To Month Name Use:

dim MyMonthNumber as integer = 7
MonthName(MyMonthNumber)

or

Format(New Date(2009, MyMonthNumber, 1), "MMMMM")

-------------------------------------------------------------------------

For Converting A Number To Month Name Use:

dim MyMonthName as String = "July"
CDate("01-" & MyMonthName & "-2009").Month

Get Country Name by Client IP

Add WebService Reference of

http://www.webservicex.net/geoipservice.asmx?wsdl

Here is the code:

Dim objWebServiceX As New webservicex.GeoIPService
Dim countryName As String = objWebServiceX.GetGeoIP(Request.ServerVariables("REMOTE_ADDR")).CountryName
Response.Write(countryName & "
")

Write To A File on Server in ASP.NET

Public Shared Function SaveTextToFile(ByVal strData As String, ByVal fname As String) As Boolean
Try
Dim fs As New FileStream(fname, FileMode.Create, FileAccess.Write)
Dim s As New StreamWriter(fs)
s.BaseStream.Seek(0, SeekOrigin.End)
s.WriteLine(strData)
s.Close()
Return True
Catch ex As Exception
' your exception handler here
End Try
Return False
End Function

VB.NET: Read Registry Value

Imports Microsoft.Win32

dim MyValue as string
MyValue = RegValue(RegistryHive.LocalMachine, "SOFTWARE\Microsoft\Internet Explorer", "Version")

Public Function RegValue(ByVal Hive As RegistryHive, ByVal Key As String, ByVal ValueName As String, Optional ByRef ErrInfo As String = "") As String
Dim objParent As RegistryKey
Dim objSubkey As RegistryKey
Dim sAns As String
Select Case Hive
Case RegistryHive.ClassesRoot
objParent = Registry.ClassesRoot
Case RegistryHive.CurrentConfig
objParent = Registry.CurrentConfig
Case RegistryHive.CurrentUser
objParent = Registry.CurrentUser
Case RegistryHive.DynData
objParent = Registry.DynData
Case RegistryHive.LocalMachine
objParent = Registry.LocalMachine
Case RegistryHive.PerformanceData
objParent = Registry.PerformanceData
Case RegistryHive.Users
objParent = Registry.Users
End Select
Try
objSubkey = objParent.OpenSubKey(Key)
'if can't be found, object is not initialized
If Not objSubkey Is Nothing Then
sAns = (objSubkey.GetValue(ValueName))
End If
Catch ex As Exception
sAns = "Error"
ErrInfo = ex.Message
Finally
If ErrInfo = "" And sAns = "" Then
sAns = "No value found for requested registry key"
End If
End Try
Return sAns
End Function

ASP.NET: Force Page to Use secure connection HTTPS

Sometimes we want to provide secure channel over an insecure network. e.g. for payment transactions. In that case we need HTTPS connection

Assume that your page is as follows: http://www.mydomain.com/mypage.aspx

Just add following code inside Load Event Handler!

If Not Request.IsSecureConnection Then
Response.Redirect("https://www.mydomain.com/mypage.aspx ")
End If