问题
Problem
When I try to add a migration to my code, e.g: dnx ef migrations add initial
,
the env.WebRootPath
in Startup(IHostingEnvironment env)
is null.
This will give me compilation errors when adding a new migration or updating the database.
The 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 of the Startup.cs I resolve my 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 build, run, and deploy the code fine. Everything works!
However I made a change in the model and want to add a migration with this command:
dnx ef migrations add MyMigration
then I see the compilation errors in the package manager console.I am using ASP 5 web application, and Entity Framework 7
回答1:
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.
回答2:
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");
}
回答3:
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.
回答4:
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("");
}
回答5:
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!)
回答6:
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.
来源:https://stackoverflow.com/questions/35322136/ihostingenvironment-webrootpath-is-null-when-using-ef7-commands