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?
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:
services.AddSingleton(HostingEnvironment.ContentRootFileProvider);
to register an IFileProvider service, also described in the docsThen 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With