What the simplest way to convert a file path to a absolute url. Example:
C:\myapp\src\SqlExpress\wwwroot\data\images\test.jpg
url:
http://localhost/data/images/test.jpg
This is what I did. I never could find a way to easily find the wwwroot path outside a controller. So I used a static variable in Startup class, which is accessible throughout the application.
public class Startup
{
public static string wwwRootFolder = string.Empty;
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Startup.wwwRootFolder = env.WebRootPath;
// ...
}
}
Then in wherever I want..
public static string GetUrlFromAbsolutePath(string absolutePath)
{
return absolutePath.Replace(Startup.wwwRootFolder, "").Replace(@"\", "/");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With