Prabhakaran
Prabhakaran

Reputation: 92

how to find a Last Modified Date of a webPage?

i have a problem in my project to find the Last Modified date of a site..

is any code to find that in asp.net

thanks in advance..

Upvotes: 2

Views: 2148

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460288

FileInfo.LastWriteTime should give you what you need:

System.IO.File.GetLastWriteTime(Request.PhysicalPath).ToString();

According to your comment on the other answer, you instead want to get the last modified time of any web-site (not your own ASP.NET page). You could use Net.HttpWebRequest to request a given URL to get the LastModified property of the HttpResponse:

Protected Sub GetLastModifiedTimeOfWebPage(sender As Object, e As EventArgs)
    Dim url = Me.TxtURL.Text.Trim
    If Not url.StartsWith("http:") Then url = "http://" & url
    Dim ResponseStatus As System.Net.HttpStatusCode
    Dim lastModified As Date
    Try
        lastModified = RequestLastModified(url, ResponseStatus)
    Catch ex As System.Exception
        ' log and/or throw
        Throw
    End Try
    If ResponseStatus = Net.HttpStatusCode.OK Then
        Me.LblLastModified.Text = lastModified.ToString
    End If
End Sub

Public Shared Function RequestLastModified( _
    ByVal URL As String, _
    ByRef retStatus As Net.HttpStatusCode
) As Date
    Dim lastModified As Date = Date.MinValue
    Dim req As System.Net.HttpWebRequest
    Dim resp As System.Net.HttpWebResponse
    Try
        req = DirectCast(Net.HttpWebRequest.Create(New Uri(URL)), Net.HttpWebRequest)
        req.Method = "HEAD"
        resp = DirectCast(req.GetResponse(), Net.HttpWebResponse)
        retStatus = resp.StatusCode
        lastModified = resp.LastModified
    Catch ex As Exception
        Throw
    Finally
        If resp IsNot Nothing Then
            resp.Close()
        End If
    End Try

    Return lastModified
End Function

Note: Many sites lie with this property and return only the current time.

Upvotes: 0

munnster79
munnster79

Reputation: 578

Check out this question

How can you mine the "Last Modified Date" for an ASP.NET page which is based on a Master Page?

the basic code you need is this

Dim strPath As String = Request.PhysicalPath
Label1.Text = "Modified: " + System.IO.File.GetLastWriteTime(strPath).ToString()

Upvotes: 1

Related Questions