Core: FileVersionProvider Class missing

Created on 30 Dec 2018  路  6Comments  路  Source: dotnet/core

Bug Description

FileVersionProvider Class missing.

In my code, under .NET Core 2.1, I used FileVersionProvider class in a TagHelper that calls the AddFileVersionToPath method without any problem.
Today, after upgrading to .NET Core 2.2.101 FileVersionProvider wasn't found in Microsoft.AspNetCore.Mvc.TagHelpers.Internal NS.

Any help will be much appreciated

area-aspnet

Most helpful comment

Figured it out.
You just need to Dependency inject an IFileVersionProviderand it will work automagically. In my case, since I'm working in a static class and method (with a Razor page for context), I don't dependency inject but get it using GetRequiredService

Instead of:
```c#
public static string AddFileVersionToPath(this IRazorPage page, string path)
{
var context = page.ViewContext.HttpContext;
IMemoryCache cache = context.RequestServices.GetRequiredService();
var hostingEnvironment = context.RequestServices.GetRequiredService();
var versionProvider = new FileVersionProvider(hostingEnvironment.WebRootFileProvider, cache, context.Request.Path);
return versionProvider.AddFileVersionToPath(path);
}
}

It is now:

```c#
public static string AddFileVersionToPath(this IRazorPage page, string path)
        {
            var context = page.ViewContext.HttpContext;
            IFileVersionProvider fileVersionProvider = context.RequestServices.GetRequiredService<IFileVersionProvider>();
            return fileVersionProvider.AddFileVersionToPath(context.Request.PathBase, path);
        }

And the IFileVersionProvider is in the following namespace

using Microsoft.AspNetCore.Mvc.ViewFeatures;

Additionally in Startup.cs I think you will need services.addMVC

All 6 comments

I have the same issue

Based on the changes here: https://github.com/aspnet/Mvc/pull/8455/commits/00d6771f73394def3696e39d344a1de2212bc5d5

It seems like the FileVersionProvider was replaced with DefaultFileVersionProvider. The namespace is also different. Now in Microsoft.AspNetCore.Razor.Infrastructure. DefaultFileVersionProvider is internal so you can't use it directly. Seems to relate to the following issue. https://github.com/aspnet/Mvc/issues/6371

I will update here if I figure out how to make use of it.

Figured it out.
You just need to Dependency inject an IFileVersionProviderand it will work automagically. In my case, since I'm working in a static class and method (with a Razor page for context), I don't dependency inject but get it using GetRequiredService

Instead of:
```c#
public static string AddFileVersionToPath(this IRazorPage page, string path)
{
var context = page.ViewContext.HttpContext;
IMemoryCache cache = context.RequestServices.GetRequiredService();
var hostingEnvironment = context.RequestServices.GetRequiredService();
var versionProvider = new FileVersionProvider(hostingEnvironment.WebRootFileProvider, cache, context.Request.Path);
return versionProvider.AddFileVersionToPath(path);
}
}

It is now:

```c#
public static string AddFileVersionToPath(this IRazorPage page, string path)
        {
            var context = page.ViewContext.HttpContext;
            IFileVersionProvider fileVersionProvider = context.RequestServices.GetRequiredService<IFileVersionProvider>();
            return fileVersionProvider.AddFileVersionToPath(context.Request.PathBase, path);
        }

And the IFileVersionProvider is in the following namespace

using Microsoft.AspNetCore.Mvc.ViewFeatures;

Additionally in Startup.cs I think you will need services.addMVC

Great work. Thanks!!

@leitocardoso maybe it won't be needed, but in future, I recommend to report bugs with minimal repro steps / code.

@karelz thanks for your advice. I thought that my message was pretty self-explanatory and didn't require portions of code. Next time I'll do that.

Was this page helpful?
0 / 5 - 0 ratings