How to obtain path to Razor Class Lib static assets in controller/library code?

Viewed 28

I am migrating an existing .NET Framework application to .NET 6. Parts of the application were UI components in library projects, which I have updated to be Razor Class Libraries. I have updated the relevant projects based on these microsoft docs:
consume-a-razor-component-from-an-rcl
razor-pages/ui-class

However, I have found a situation where an image, which exists in the RCL project(s), was being loaded in-memory, and modified using the Graphics/Bitmap APIs. I am trying to discover how to accomplish this in the .NET 6 world. I have such static assets properly loading in Views and such at runtime, but I suppose I am wondering how to leverage that runtime lookup in library code (as opposed to in Razor view code).

For example, given an image path to a content file from an RCL project like: ~/_content/My.RCL.Project/Images/foo.png

I can reference such a file in an image tag, like: <img src="~/_content/My.RCL.Project/Images/foo.png" />

And this works just fine as long as the path is correct - the image is loaded as expected. This is being done successfully for many images in the site thus far.

But how can I load that file into memory in server-side/library code, e.g.

const string filePath = "~/_content/My.RCL.Project/Images/foo.png";
using(var bitmap = new Bitmap(filePath))
using(var graphics = Graphics.FromImage(bitmap))
{
    //... omitted
}

I have tried to find the physical file via the IWebHostEnvironment APIs:

IWebHostEnvironment env; //obtained via DI
env.WebRootFileProvider.GetFileInfo(filePath);
env.ContentRootFileProvider.GetFileInfo(filePath);

However, both of these methods return a non-existent file.

Does such a mechanism/API exist for RCL content? Can this be done by calling the Razor engine directly in some form?

0 Answers
Related