Skip to main content

Overview

The HelperPathProvider class provides methods to map file names to physical paths and URL paths within the web application’s folder structure. It supports multiple predefined folders for organizing different types of files. Namespace: MvcNetCoreUtilidades.Helpers

Folders Enum

public enum Folders
{
    Images,
    facturas,
    Uploads,
    Temporal
}
Defines the available folder types for file operations:
  • Images - Maps to “images” folder
  • facturas - Maps to “facturas” folder
  • Uploads - Maps to “uploads” folder
  • Temporal - Maps to “temp” folder

Constructor

public HelperPathProvider(IWebHostEnvironment hostEnvironment, IServer server)
Initializes a new instance of the HelperPathProvider class.
hostEnvironment
IWebHostEnvironment
required
The web host environment used to access the application’s root path
server
IServer
required
The server instance used to access server addresses for URL generation

Methods

MapPath

public string MapPath(string FileName, Folders folder)
Maps a file name to its physical path on the server’s file system.
FileName
string
required
The name of the file (including extension)
folder
Folders
required
The folder type where the file is located (from the Folders enum)
Returns: string - The complete physical path to the file Usage:
string path = pathProvider.MapPath("logo.png", Folders.Images);
// Returns: /wwwroot/images/logo.png

MapUrlPath

public string MapUrlPath(string fileName, Folders folder)
Maps a file name to its URL path accessible via HTTP.
fileName
string
required
The name of the file (including extension)
folder
Folders
required
The folder type where the file is located (from the Folders enum)
Returns: string - The complete URL path to the file Usage:
string url = pathProvider.MapUrlPath("logo.png", Folders.Images);
// Returns: http://localhost:5000/images/logo.png

Folder Mappings

Enum ValuePhysical Folder
Folders.Imagesimages
Folders.facturasfacturas
Folders.Uploadsuploads
Folders.Temporaltemp

Dependency Injection

Register the helper in your Program.cs:
builder.Services.AddTransient<HelperPathProvider>();
Then inject it into your controllers or services:
public class UploadFilesController : Controller
{
    private HelperPathProvider pathProvider;
    
    public UploadFilesController(HelperPathProvider pathProvider)
    {
        this.pathProvider = pathProvider;
    }
}

Build docs developers (and LLMs) love