ASP.NET MVC getting last modified date/FileInfo of View
Asked Answered
V

3

5

I'm required to include the last modified date on every page of my applications at work. I used to do this by including a reference to <%= LastModified %> at the bottom of my WebForms master page which would return the last modified date of the current .aspx page. My code would even check the associated .aspx.cs file, compare the last modified dates, and return the most recent date.

Does anyone know if you can read the FileInfo of a MVC View? I would like to include it in the master page, if possible.

I have a base controller that all wired up and ready to go. All I need to know is how to access the FileInfo of the current view.

namespace MyMVCApp.Controllers
{
    public abstract class SiteController : Controller
    {
        public SiteController()
        {
            ViewData["modified"] = NEED TO GET FILEINFO OF CURRENT VIEW HERE;
        }
    }
}
Valerianaceous answered 7/10, 2009 at 4:3 Comment(2)
There is no default view for controller. Controller can show any view calling View("view_name").Monomial
Is the modified date purely coming from the .aspx's (the view's) last modified date? You could probably do something there but usually that sort of stuff (last modified date) comes from your persistent storage. Is your site not going to be database driven but more of a static site?Bursitis
G
7

You need to know the physical file of the View, which is only known when the view is being processed, so we delay the work until then:

At the bottom of the view file, just add:

Last Modified Date: @File.GetLastWriteTime(this.Server.MapPath(this.VirtualPath))

NOTE: It must be in the view file you want the date for. If you put it in the layout file, it will give you the date of that file. However, you can get the date into the footer using a section

In View:

@section lastwrite
{
    Last Modified Date: @File.GetLastWriteTime(this.Server.MapPath(this.VirtualPath))
}

in layout:

@RenderSection("lastwrite", required: false)
Gent answered 1/9, 2015 at 16:59 Comment(2)
Hmmm... The question was asked in 2009, got it's first (wrong) answer two years later in 2011, got it's second (working, I think, but quite involved) answer two years after that. And now, two years after THAT, I've posted my (simpler) answer. (Don't think i'm gonna ring up many rep points on this...)Gent
This was really helpful. For better representation you could add .GetLastWriteTime(...).ToLongDateString().Cyclist
D
1

The following will give you the date of the last time the view was written:

// Last Modified Date
var strPath = Request.PhysicalPath;
ViewBag.LastUpdated = System.IO.File.GetLastWriteTime(strPath).ToString();

Noticed that I used ViewBag instead of ViewData.

Declaim answered 6/9, 2011 at 20:40 Comment(1)
Have you even tried this? Name of view is determined later on, it is never available here.Enterpriser
T
0

Try this:

private DateTime? GetDate(string controller, string viewName)
{
    var context = new ControllerContext(Request.RequestContext, this);
    context.RouteData.Values["controller"] = controller;
    var view = ViewEngines.Engines.FindView(context, viewName, null).View as BuildManagerCompiledView;
    var path = view == null ? null : view.ViewPath;
    return path == null ? (DateTime?) null : System.IO.File.GetLastWriteTime(path);
}
Tweeddale answered 9/12, 2013 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.