having a helper method like this:
public static IHtmlContent Source(this IHtmlHelper html, string s)
{
var path = ServerMapPath() + "Views\\" + s;
I need to get the equivalent of Server.MapPath in asp.net core
The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
WebRootPath is the base path for all the static files served by the application, such as CSS, JavaScript or Image files.
IWebHostEnvironment is included in the Microsoft. AspNetCore. Hosting package, you simply need to add it as a reference to your project by right clicking on your project file and selecting 'Manage Nuget Packages' then searching for Microsoft.
Server.MapPath Equivalent in ASP.NET Core. Web Developers who use Microsoft technologies have always relied on the Server.MapPath method to resolve file paths in classic ASP and ASP.NET Web Forms and MVC versions up to and including 5.
Web Developers who use Microsoft technologies have always relied on the Server.MapPath method to resolve file paths in classic ASP and ASP.NET Web Forms and MVC versions up to and including 5. This method has not been included in ASP.NET Core, so what do you use instead?
Microsoft has permanently removed Server.MapPath function from .Net Core and introduced a new interfaces IHostingEnvironment for .Net Core 2.0 and IWebHostEnvironment for .Net Core 3.0. The IHostingEnvironment is an interface for .Net Core 2.0 and IWebHostEnvironment has replaced IHostingEnvironment in .Net Core 3.0.
But this method is no longer available in asp .net core 5.0 MVC. The is to use IHostEnvironment The solution is to inject IHostEnvironment in either the HomeController or other Controllers or in the startup.cs file. The ContentRootPath resolves the application's base path.
You need to inject IHostingEnvironment
. Then:
var path = env.ContentRootPath + "Views\\" + s;
in an html helper you can do this:
((IHostingEnvironment)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath;
I recommend Don't use a static class. You can keep something similar to your class, and register it as a singleton.
public class SourceClass
{
private readonly IWebHostEnvironment _webHostEnvironment;
public SourceClass (IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment= webHostEnvironment;
}
private IHtmlContent Source(this IHtmlHelper html, string s)
{
string contentRootPath = _webHostEnvironment.ContentRootPath;
Path.Combine(contentRootPath,"Views",s)
}
}
Then, in Startup.ConfigureServices:
services.AddSingleton<SourceClass>();
Finally, inject SourceClass
where you need it, instead of just statically referencing the class.
(Note :@regnauld provided this solution, but it had some drawbacks, which I thought would be fully answered)
But if you want to use a static method you can do(Note: In .Net Core 3 IHostingEnvironment
is absolute and IWebHostEnvironment
must be used instead ):
for ServerPath ,use
((IWebHostEnvironment)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment))).ContentRootPath
OR
for WebRootPath (wwwroot) ,use
((IWebHostEnvironment)html.ViewContext.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment))).WebRootPath
in this answer you can find more details.
I also recommend that you use this code:
var path = Path.Combine(WebRootPath,"Views",s)
instead of
var path = WebRootPath + "Views\\" + s
to run the code on all operating systems.
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