IHostingEnvironment.WebRootPath is null when using EF7 commands

大城市里の小女人 提交于 2019-12-05 02:16:07

The env.WebRootPath can also be null if the wwwroot folder has been inadvertantly removed from the root of your project. Adding a wwwroot folder to the root of your project will also fix this issue.

There is an issue reported on github regarding my problem:

https://github.com/aspnet/EntityFramework/issues/4494

I used the workaround in the comments now it seems to be working fine:

if (string.IsNullOrWhiteSpace(_env.WebRootPath))
{
   env.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}

In my case, the problem was that the wwwroot folder was empty and VS didn't recognized it.

The solution: create a placeholder.txt in wwwroot.

Hope it helps.

I got the error while file uploading in asp.net core application with Telerik controls. The error is not related to Telerik control.

While investigation I got the description that This is the variable which gives the same functionality as Server.MapPath given in asp.net

Good explanation:- What is the equivalent of Server.MapPath in ASP.NET Core?

//this code from above link that how it will be used in the code
public class HomeController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public ActionResult Index()
    {
        string webRootPath = _hostingEnvironment.WebRootPath;
        string contentRootPath = _hostingEnvironment.ContentRootPath;

        return Content(webRootPath + "\n" + contentRootPath);
    }
}

This is the actual code which gives me the error, I replace the WebRootPath with another way to solve the issue and it works. you can assign and use in the entire application.

public async Task<ActionResult> SaveAsync(IEnumerable<IFormFile> files)
        {
            // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

                    // Some browsers send file names with full path.
                    // We are only interested in the file name.
                    var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));

                    //The below line gives the error
                    //var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);

                    //and I replace with this (but we can use this variable at multiple location by assign a physical path)
                    var physicalPath = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"), "App_Data", fileName);

                    //The files are not actually saved in this demo
                    using (var fileStream = new FileStream(physicalPath, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                }
            }

            // Return an empty string to signify success
            return Content("");
        }

I was faced with the same issue but with what seems a unique scenario to this post, so I'll add it.

I have a solution with several nested projects. Similar to this:

> Solution
  > MvcProject
    > wwwroot
    > // etc.
  > SecondMvcProject
  > AnotherMvcProject

When running my MvcProject, the IHostingEnvironment had the ContentRootPath set to the Solution folder, and the WebRootPath was null.

Per other people's answers, adding a wwwroot folder in the Solution folder made WebRootPath have a value, but it was the Solution folder path. I had to modify my launch task's current working directory (cwd) in VS Code. It defaulted to "cwd": "${workspaceFolder}", and I had to change it to "cwd": "${workspaceFolder}/MvcProject"

Update

I had this same problem again on another machine, and was glad I commented on this. This time, the problem was that we didn't have any files in one of our wwwroot folders, and empty folders don't get committed to git repos. Add a file to your folder, if you need it!

(It was there for consistency with setup for static file handling. We'll eventually have something in there before the project launches!)

M.Nour Berro

In case someone still faces this problem, in my case the issue was default constructor which I created it for a test.

So, remove the default constructor if any.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!