Reputation: 3803
We would like to embed some code in each ASP.NET page that queries its "Last Modified Date" and displays it at the bottom of the page.
In the past, we've relied on the person making any changes to the page to manually update the "This page last modified on (todays date)" text at the bottom of the page. Many times they forget to update this, which is causing some confusion as to when the information was last updated on that particular page. Since the site is not based on a CMS which could store this information in its back-end database, we're trying to do the determination as to when the page was last saved from the file system on the server and include that date in the text of the page.
I'm not sure how a page's being based on a Master Page plays into the "last modified date". What we're really looking for is for the content page file's LMD get queried so we can embed that in the text of the page and not the LMD of the Master Page it's based on.
Thanks!
Upvotes: 4
Views: 7126
Reputation: 503
If your files compiled into a dll you can get assembly's last modified date, code taken from here and store it in a shared cache for 1 hour:
<div class="footer text-center">
<em>
<%
try
{
var CSAdmin_ModDate = HttpRuntime.Cache.Get("CSAdmin_ModDate");
if (Request.ServerVariables["SCRIPT_NAME"] == "/cs_admin/default.aspx" && CSAdmin_ModDate == null)
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;
Response.Write(" (modified " + lastModified.ToShortDateString() + ")");
HttpRuntime.Cache.Insert("CSAdmin_ModDate", lastModified.ToShortDateString() , null, DateTime.UtcNow.AddMinutes(60), Cache.NoSlidingExpiration);
}else if(Request.ServerVariables["SCRIPT_NAME"] == "/cs_admin/default.aspx" && CSAdmin_ModDate != null)
{
Response.Write(" (modified " + CSAdmin_ModDate + ")");
}
}
catch (Exception ex)
{ }
%>
</em>
</div>
Upvotes: 0
Reputation: 50728
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.
Upvotes: 1
Reputation: 3803
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)
Upvotes: 7
Reputation: 626
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:
Here's a link to the FileInfo class: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx
Good luck.
Matti
Upvotes: 2