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

落爺英雄遲暮 提交于 2019-12-06 01:51:14

Figured I'd post the answer to my question so that others can benefit.

My solution was to add a label control to your Master Page where you want the "Modified: + date saved" information displayed. We put ours in the footer:

Modified: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Then create a "Page_Load" event to the Master Page code behind (this website uses code behind and VB) and add the following code:

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

When the page loads, it will execute the code above and replace the "Label" text with the date the file was last saved to disk.

Hope this helps.

(If you know a better way, feel free to educate us in a comment)

You are not going to be able to get the last modified date from the file system on a web server from javascript. this is executed client side and has nothing to do with the last time the physical aspx page was modified.

Here is what I would suggest:

  1. Create a new base page class by simply inheriting from System.Web.UI.Page
  2. In your base page class use Request.PhysicalPath to get the full page to the current page and create a new FileInfo object using that path.
  3. Call the FileInfo object's Refresh() method to get the last modified date (this can be cached).
  4. Write out the LastWriteTime property to get the last time it was modified.
  5. Make sure the pages you want to write out the last modified date inherit from the new base class!

Here's a link to the FileInfo class: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

Good luck.

Matti

You'd have to use server side code and a mix of the FileInfo object to get the modified date: http://www.communitymx.com/content/article.cfm?page=4&cid=06BF2 with getting the file path using Server.MapPath("~/virtual/page.aspx") to get the currently executing page.

I know you want to make it generic, so I believe you can use Server.MapPath with Request.ServerVariables.Get("SCRIPT_NAME") or another variable to do it generically.

HTH.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!