user15529787
user15529787

Reputation:

WebRootPath not part of ASP.NETCORE hosting?

I am trying to help return the path of my environment, however the tutorial I am following along to seems to be outdated, my primary question here is whether or not it is the same if WebRootPath can be replaced by ContentRootPath in my code.

I apologize if my question seems kind of elementary, and is there somewhere I can get the quick notes on migrating to the newer version of .NET, as I understand it is now called. I am linking my code with the error message below.

Thanks a lot in advance!!

if (model.ImageUrl != null && model.ImageUrl.Length > 0)
                {
                    var uploadDir = @"images/employee";
                    var fileName = Path.GetFileNameWithoutExtension(model.ImageUrl.FileName);
                    var extension = Path.GetExtension(model.ImageUrl.FileName);
                    var webRootPath = _hostingEnvironment.WebRootPath;

Error message:

Severity Code Description Project File Line Suppression State Error CS1061 'HostingEnvironment' does not contain a definition for 'WebRootPath' and no accessible extension method 'WebRootPath' accepting a first argument of type 'HostingEnvironment' could be found (are you missing a using directive or an assembly reference?) Paycompute C:\Projects\PaycomputeApp\Paycompute\Controllers\EmployeeController.cs 77 Active

Upvotes: 0

Views: 2158

Answers (1)

user15529787
user15529787

Reputation:

So I did a little digging and am only leaving this here for other beginners who may run into the same problem.
The HostingEnvironment interface has been depreciated since in version 3.0 and later, this can be solved by updating the interface that you are using.

I am demonstrating this below!! Incorporate the following using statement.

using Microsoft.AspNetCore.Mvc;

and then change the incorporated interface like so (at least in my use case)

 private readonly IWebHostEnvironment _hostingEnvironment;

    public EmployeeController(IEmployeeService employeeService, IWebHostEnvironment hostingEnvironment)

Upvotes: 1

Related Questions