Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)

I am using .NET Core 3.0.100 ; Microsoft Visual Studio Community 2019 Preview Version 16.4.0 Preview 1.0 ; Blazor-server (official release).

I am trying to add Authentication and Authorization to my Blazor-server web app. I am reading the guideline at here https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.0&tabs=visual-studio#scaffold-identity-into-an-empty-project

(I also read this https://github.com/aspnet/Identity/issues/1825)

Then I right-click on Project, choose Add \ New Scaffolded Item...

I read the file ScaffoldingReadme.txt, then follow the guide.

I press F5 for debugging, I catch the error.

Severity:       Error
Error Code:     CS0246
Description:    The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)
Project:        foo
File:       C:\Users\donhuvy\Desktop\foo\bar\obj\Debug\netcoreapp3.0\Razor\Pages\Shared\_Layout.cshtml.g.cs
Line:       455
Suppression State:  Active

Screenshot Screenshot of vscode with the error

Because file \obj\Debug\netcoreapp3.0\Razor\Pages\Shared\_Layout.cshtml.g.cs stand inside Razor Class Library Microsoft.AspNetCore.Identity.UI (3.0.0), I cannot edit it.

How to fix it?

like image 828
Do Nhu Vy Avatar asked Oct 02 '19 14:10

Do Nhu Vy


People also ask

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 IHostingEnvironment .NET core?

The IHostingEnvironment is an interface for . Net Core 2.0. The IHostingEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IHostingEnvironment interface have two properties.


2 Answers

This is an issue,

The code generate is

Microsoft.AspNetCore.Hosting
@using Microsoft.AspNetCore.Mvc.ViewEngines
@inject IWebHostEnvironment Environment
@inject ICompositeViewEngine Engine

it is missing @using

it should be

@using Microsoft.AspNetCore.Hosting

I reported issue at https://github.com/aspnet/Scaffolding/issues/1094

like image 70
Do Nhu Vy Avatar answered Oct 19 '22 11:10

Do Nhu Vy


Issue solved by adding in our csproj file (3.1 core app):

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
like image 20
Drilon Ahmetaj Avatar answered Oct 19 '22 12:10

Drilon Ahmetaj