Home > other >  How to get an absolute path in ASP.NET Core MVC app from an ApiController?
How to get an absolute path in ASP.NET Core MVC app from an ApiController?

Time:12-06

I'm attempting to re-write an old .Net framework 4.5 web app to an ASP.NET Core MVP app. The existing code can access an absolute path like so and can pass it to a web view.

page.cshtml:

<head>
    @{
        var version = new VersionController().Get();
    }
</head>

VersionController.cs:

public class VersionController : ApiController
{
    public IEnumerable<string> Get()
    {
        var results = new List<string>();
        
        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(HostingEnvironment.MapPath("~")   @"\bin\Application.Common.dll");
        results.Add(string.Format("{0} ({1})", fileVersionInfo.FileDescription, fileVersionInfo.FileVersion));

        return results;
    }
}

How can this be achieved in a .NET 6 MVC app?

I think I could create a controller like so but I'm unsure how to create the constructor in the .cshtml page.

public class VersionController : ApiController
{       
    private readonly IWebHostEnvironment _webHostEnvironment;

    public VersionController(IWebHostEnvironment webHostEnvironment)
    {
        _webHostEnvironment = webHostEnvironment;
    }
    
    public IEnumerable<string> Get()
    {
        var results = new List<string>();

        string webRootPath = _webHostEnvironment.WebRootPath;

        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(webRootPath, @"\bin\Application.Common.dll"));
        results.Add(string.Format("{0} ({1})", fileVersionInfo.FileDescription, fileVersionInfo.FileVersion));

        return results;
    }
}

CodePudding user response:

use the IWebHostEnvironment service to get the absolute path of your application's web root directory.

public class VersionController : ApiController
{       
    private readonly IWebHostEnvironment _webHostEnvironment;

    public VersionController(IWebHostEnvironment webHostEnvironment)
    {
        _webHostEnvironment = webHostEnvironment;
    }
    
    public IEnumerable<string> Get()
    {
        var results = new List<string>();

        // Get the absolute path of the web root directory
        string webRootPath = _webHostEnvironment.WebRootPath;

        // Use the web root path to access a file in the bin directory
        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(webRootPath, @"\bin\Application.Common.dll"));
        results.Add(string.Format("{0} ({1})", fileVersionInfo.FileDescription, fileVersionInfo.FileVersion));

        return results;
    }
}

use the @inject directive to inject the IWebHostEnvironment service and create an instance of the VersionController class.

<head>
    @{
        var versionController = new VersionController();
    }
</head>

Eliminate FileNotFoundException error when using the Path.Combine method to construct the file path.

string filePath = Path.Combine(webHostEnvironment.ContentRootPath, "bin", "Application.Common.dll");
  • Related