IHostingEnvironment.WebRootPath is null when using EF7 commands

Problem

When I try to add a jump to my code, for example: dnx ef migrations add initial ,

env.WebRootPath in Startup(IHostingEnvironment env) is null.

This will give me compilation errors when adding a new migration or updating the database.

Code

In the Startup.cs class, I have these lines in the constructor:

 public Startup(IHostingEnvironment env) { // ... MyStaticClass.Initialize(env.WebRootPath); // ... _hostingEnvironment = env; } 

Here env.WebRootPath is null and in the Initialize function this throws an exception.

In the ConfigureServices function for Startup.cs, I enable class dependencies:

 public void ConfigureServices(IServiceCollection services) { // ... services.AddInstance<IMyService>(new MyService(_hostingEnvironment.WebRootPath)) // Note: _hostingEnvironment is set in the Startup constructor. // ... } 

Notes

  • I can create, run and deploy the code in order. Everything works!

  • However, I made changes to the model and want to add the migration using this command: dnx ef migrations add MyMigration , then I see compilation errors in the package manager console.

  • I am using ASP 5 web application and Entity Framework 7

+7
source share
6 answers

There is a post on github about 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"); } 
+9
source

Env.WebRootPath can also be null if the wwwroot folder was accidentally deleted from the root of your project. Adding the wwwroot folder to the root of your project will also fix this problem.

+21
source

In my case, the problem was that the wwwroot folder was empty and VS did not recognize it.

Solution: create a placeholder.txt file in wwwroot.

Hope this helps.

+4
source

In case someone is still facing this problem, in my case the problem was the default constructor that I created for the test.

So, remove the default constructor, if any.

0
source

I got an error while loading a file in the main asp.net application with Telerik controls. The error is not related to the management of Telerik.

During the investigation, I received a description that this is a variable that provides the same functions 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 that gives me an error, I am replacing WebRootPath with another way to solve the problem, and it works. You can designate and use throughout the 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(""); } 
0
source

I ran into the same problem, but with a unique script in this post, so I will add it.

I have a solution with several nested projects. It looks like this:

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

When I started my MvcProject in IHostingEnvironment for ContentRootPath , the Solution folder was set, and WebRootPath was null.

According to other people's answers, adding the wwwroot folder to the Solution folder made the value WebRootPath , but that was the path to the Solution folder. I had to change the current working directory of my startup task ( cwd ) in VS Code. By default it is "cwd": "${workspaceFolder}" , and I had to change it to "cwd": "${workspaceFolder}/MvcProject"

Refresh

I again had the same problem on another machine, and I was glad I commented on this. This time, the problem was that we did not have files in one of our wwwroot folders, and empty folders did not wwwroot up in the git repositories. Add the file to your folder if you need it!

(This was done in order to be consistent with setting static file processing. In the end, we will have something there before the project starts!)

0
source

Source: https://habr.com/ru/post/1242718/


All Articles