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

妖精的绣舞 提交于 2019-11-30 22:29:07

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)

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.

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