Getting relative path to `bin\Debug\netcoreapp2.2` in ASP.NET Core 2

If you need the relative path of the executing assemblies, you will probably need something like this: ‘~\bin\Debug\netcoreapp2.2’


I recently had to do this because I had to render a razor view in an ASP.NET Core 2 application.

Tutorial here:

Issue was that I had  to overcome a bug( that needed the relative netcodeapp2.2 that the asemblied resided in.


var viewPath = ‘~/bin/Debug/netcoreapp2.2/Views/MainView.cshtml’;

_viewEngine.GetView(executingFilePath: viewPath , viewPath: viewPath , isMainPage: true);

Getting the `bin\Debug\netcoreapp2.2` can be achieved by using this code:


using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
public class RenderingService : IRenderingService
	private readonly IHostingEnvironment _hostingEnvironment;
	public RenderingService(IHostingEnvironment hostingEnvironment)
	_hostingEnvironment = hostingEnvironment;

	public string RelativeAssemblyDirectory()
		var contentRootPath = _hostingEnvironment.ContentRootPath;
		string executingAssemblyDirectoryAbsolutePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
		string executingAssemblyDirectoryRelativePath = System.IO.Path.GetRelativePath(contentRootPath, executingAssemblyDirectoryAbsolutePath);
		return executingAssemblyDirectoryRelativePath;

Code Beautifier:

Leave a Reply

Your email address will not be published. Required fields are marked *