Reputation: 622
I want to put the last modified date of (the .aspx file responsible for) the currently viewed page into a footer of a Web Forms site.
I am working on a default Web Forms template from Visual Studio 2015 and building the project for .NET 4.5. In Site.Master
, I have modified the footer like so:
<footer>
<p>
Page last updated on:
<asp:Label ID="modyfikacja" runat="server" Text="coś nie poszło" />
</p>
</footer>
and modified Page_Load()
method in Site.Master.cs
like so:
protected void Page_Load(object sender, EventArgs e)
{
string _site = Server.MapPath(HttpContext.Current.Request.Url.AbsolutePath);
modyfikacja.Text = "(" + _site + ") " + File.GetLastWriteTime(_site).ToString();
}
Unfortunately, that doesn't actually work all the way through:
http://localhost:11111
, it correctly returns date for file path C:\imaginelikeapathheredude\default.aspx
,http://localhost:11111/About
, it attempts to get the date for file path C:\imaginelikeapathheredude\About
- ie. web route pasted onto physical root, instead of the C:\imaginelikeapathheredude\about.aspx
file behind the route,http://localhost:11111/contact.aspx
redirects to http://localhost:49480/contact
and doesn't change the output of any functions mentioned in this question).There are loads of alternative solutions to getting the file, proposed all over web (and on StackOverflow), but none of them work, either. If I change the _site
variable to...
Request.PhysicalPath
, nothing changes.Server.MapPath(HttpContext.Current.Request.Url.AbsolutePath)
, nothing changes.Server.MapPath(HttpContext.Current.Request.ServerVariables.Get("SCRIPT_NAME"))
, nothing changes.HttpContext.Current.Request.ServerVariables.Get("PATH_TRANSLATED")
, nothing changes.HttpContext.Current.Request.ServerVariables.Get("SCRIPT_TRANSLATED")
, I get the exact same file path, but with \\?\
at the front, and the application crashes, since canonical path is not a valid file path.Server.MapPath(Request.Url.LocalPath.ToString())
, nothing changes.So - how exactly can I get that .aspx
file?
PS. There is a somewhat similar question, but it seems short, vague and poorly worded - SO's Similar Questions and myself think it's about getting the .aspx
file, while its two answers think it's about getting the URL.
Upvotes: 1
Views: 989
Reputation: 1
try this below code:
string ASPXphysicalpath = Page.Request.PhysicalPath;
lblLastModified.Text = System.IO.File.GetLastWriteTime(ASPXphysicalpath).ToString();
Upvotes: 0