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

末鹿安然 提交于 2019-12-07 15:48:56

问题


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!


回答1:


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)




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5235351/how-can-you-mine-the-last-modified-date-for-an-asp-net-page-which-is-based-on

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