7

I'm porting a small MVC 5 website to MVC 6 to spot breaking changes. Stuff is breaking.

The MVC 5 code uses @File.GetLastWriteTime(this.Server.MapPath(this.VirtualPath)) to get the timestamp, as recommended here. Apparently in MVC 6, the .cshtml page no longer has Server or VirtualPath members. What's the new incantation?

4 Answers 4

4

Revisiting my own question 18 months later... the framework is now ASP.NET Core 2.0 MVC and it seems the framework, documentation and best practices have changed a bit.

You should use a FileProvider as described in the MS docs. There's no point in recreating that article here, but be sure to:

  • Add an IHostingEnvironment to the Startup constructor parameters, and save it off in a local variable, as described in the docs
  • In Startup.ConfigureServices(), call services.AddSingleton(HostingEnvironment.ContentRootFileProvider); to register an IFileProvider service, also described in the docs
  • Add an IFileProvider to the controller's constructor parameters, and save it off in a local variable

Then to actually get the last modified date, the controller will look something like this:

public class HomeController : Controller
{
    private IFileProvider _fileProvider;

    public HomeController(IFileProvider fileProvider)
    {
        _fileProvider = fileProvider;
    }

    public IActionResult Index()
    {
        DateTimeOffset lastModifiedDate = _fileProvider.GetFileInfo(@"Views\Home\Index.cshtml").LastModified;
        // use it wisely...
        return View();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

An alternative solution for @PaulWilliam's asnwer

In cases when you can't use a File provider for any reasons, getting a file's last modified date can be done using the System.IO.File static class and it's method GetLastWriteTime or GetLastWriteTimeUtc which returns a DateTime obj:

DateTime lastModified = System.IO.File.GetLastWriteTimeUtc(filePath);

Note that the non-utc method, GetLastWriteTime returns the last-modified date based on the server/filesystem time.

Other than that, and all the write-read methods, System.IO.File also contains methods that help retrieve data as last access time, creation time etc. Not only getter methods, but setters aswell.

Docs: File Class - MS Docs


Also worth pointing out that you can use the GetAttributes/SetAttributes methods to work with FileAttributes enums which are used for defining a file's status as Hidden, Compressed etc.

Docs: File.GetAttributes(String) Method - MS Docs and FileAttributes Enum - MS Docs

Comments

0

You can get ApplicationBasePath from the IApplicationEnvironment service.

private readonly IApplicationEnvironment _env;

public FileController(IApplicationEnvironment appEnv)
{
    _env= appEnv;
}

public IActionResult Index()
{
    var myModel = _env.ApplicationBasePath;
    return View(myModel);
}

Then you can make your own path calculation, like this, for example:

    public IActionResult Index()
    {
        var myFileVirtualPath = "/ab/c.d"
        var myModel = Path.Combine(_env.ApplicationBasePath, myFileVirtualPath);
        return View(myModel);
    }

Comments

-2

@(new System.IO.FileInfo(new Microsoft.Extensions.FileProviders.PhysicalFileProvider(Environment.CurrentDirectory).GetFileInfo(this.Path).PhysicalPath).CreationTime)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.