Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get server.MapPath in asp.net core from inside a helper

Tags:

asp.net-core

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

like image 808
Omu Avatar asked Jul 20 '16 14:07

Omu


People also ask

What is server MapPath in asp net?

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

What is WebRootPath?

WebRootPath is the base path for all the static files served by the application, such as CSS, JavaScript or Image files.

Where is IWebHostEnvironment?

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.

What is mappath in ASP NET Core?

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.

What do you use to resolve file paths in ASP 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. This method has not been included in ASP.NET Core, so what do you use instead?

What happened to the server mappath function?

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.

How to resolve the base path of the application in MVC?

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.


2 Answers

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;
like image 83
regnauld Avatar answered Dec 06 '22 18:12

regnauld


recommended Solution

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.


Another Solution

(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.

like image 38
Amir133 Avatar answered Dec 06 '22 20:12

Amir133