gpt4 book ai didi

c# - 错误CS0246:找不到类型或 namespace 名称 'IWebHostEnvironment'(是否缺少using指令或程序集引用?)

转载 作者:行者123 更新时间:2023-12-02 20:43:18 30 4
gpt4 key购买 nike

我正在尝试为.net应用程序运行docker build,源代码在这里:https://github.com/NileshGule/AKS-learning-series/tree/master/src

按照步骤操作后,我得到了错误:
Web/Startup.cs(30,56): error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) [/TechTalksWeb/TechTalksWeb.csproj]
我相信在.Net 3.0中已弃用,但在.Net 2.1中

dotnet --version                                                       
2.1.607

下面的构建的完整输出
➜  TechTalksWeb git:(master) ✗ docker build .                                                                                                     
Sending build context to Docker daemon 6.879MB
Step 1/11 : FROM microsoft/dotnet:2.1.300-sdk AS build-env
---> 90a5a2ee9755
Step 2/11 : WORKDIR /TechTalksWeb
---> Running in bf2efd408659
Removing intermediate container bf2efd408659
---> b5c1c0a8f026
Step 3/11 : COPY NuGet.config ./
---> 2fbe7f9eb0ba
Step 4/11 : COPY TechTalksWeb.csproj ./
---> 49e1c29e4144
Step 5/11 : RUN dotnet restore
---> Running in bfb23ac61bbf
Restoring packages for /TechTalksWeb/TechTalksWeb.csproj...
Generating MSBuild file /TechTalksWeb/obj/TechTalksWeb.csproj.nuget.g.props.
Generating MSBuild file /TechTalksWeb/obj/TechTalksWeb.csproj.nuget.g.targets.
Restore completed in 1.4 sec for /TechTalksWeb/TechTalksWeb.csproj.
Removing intermediate container bfb23ac61bbf
---> 2b5068160dd8
Step 6/11 : COPY . ./
---> d6e2551f5bfb
Step 7/11 : RUN dotnet publish --configuration Release --output releaseOutput --no-restore
---> Running in f08fbc49c68f
Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Web/Startup.cs(30,56): error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) [/TechTalksWeb/TechTalksWeb.csproj]
The command '/bin/sh -c dotnet publish --configuration Release --output releaseOutput --no-restore' returned a non-zero code: 1


有问题的行是startup.cs的一部分
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)


完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}

最佳答案

据我所记得,在.NET Core 3.0中使用了IWebHostEnvironment

在.NET Core 2.1中,您需要使用IHostingEnvironment(在3.0中已弃用)。

关于c# - 错误CS0246:找不到类型或 namespace 名称 'IWebHostEnvironment'(是否缺少using指令或程序集引用?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59704316/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com