ASP.NET MVC 获取视图的最后修改日期/文件信息

发布于 2024-08-07 09:50:03 字数 525 浏览 4 评论 0原文

我需要在工作申请的每一页上注明最后修改日期。我曾经通过引用 <%= LastModified %> 来做到这一点位于我的 WebForms 母版页的底部,它将返回当前 .aspx 页面的最后修改日期。我的代码甚至会检查关联的 .aspx.cs 文件,比较上次修改日期,并返回最近的日期。

有谁知道是否可以读取 MVC 视图的 FileInfo?如果可能的话,我想将其包含在母版页中。

我有一个基本控制器,已全部接线并准备就绪。我需要知道的是如何访问当前视图的 FileInfo。

namespace MyMVCApp.Controllers
{
    public abstract class SiteController : Controller
    {
        public SiteController()
        {
            ViewData["modified"] = NEED TO GET FILEINFO OF CURRENT VIEW HERE;
        }
    }
}

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2024-08-14 09:50:03

你需要知道View的物理文件,只有在处理视图的时候才知道,所以我们把工作推迟到那时:

在视图文件的底部,只需添加:

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

注意:它必须在视图文件中你想要的日期。如果您将其放入布局文件中,它将为您提供该文件的日期。 将日期添加到页脚

但是,您可以使用布局中的 section In View:

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

中:

@RenderSection("lastwrite", required: false)

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)
一抹淡然 2024-08-14 09:50:03

以下内容将为您提供上次写入视图的日期:

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

注意到我使用了 ViewBag 而不是 ViewData。

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.

也只是曾经 2024-08-14 09:50:03

试试这个:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文