【问题标题】:The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)命名空间“System.Web”中不存在类型或命名空间名称“UI”(您是否缺少程序集引用?)
【发布时间】:2019-12-09 06:12:31
【问题描述】:

我有asp.net core 2.2 项目。我想在我的项目中使用System.Web,但是当我尝试在我的控制器中使用它时,它会这样说

命名空间“System.Web”中不存在类型或命名空间名称“UI”(您是否缺少程序集引用?)[D:\Projects\analytics\analytics.csproj]

我有这样的 project.csproj 文件

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="IpGeoLocation.IpGeoLocation" Version="1.0.5" />
  <PackageReference Include="Microsoft.AspNetCore.App" />
  <PackageReference Include="System.Web" />
  <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" 
  />
  <PackageReference Include="MongoDB.Driver" Version="2.7.3" PrivateAssets="All" />
  <PackageReference Include="MongoDB.Driver.Core" Version="2.7.3" PrivateAssets="All" />
  <PackageReference Include="MongoDB.Bson" Version="2.7.3" PrivateAssets="All" />
</ItemGroup>

</Project>

我正在使用System.Web,因为我正在实施第三方插件ipinfo,以根据ip 地址获取用户的位置。所以在我的控制器中,我的功能就像

string ipInfoBaseUrl = "http://ipinfo.io/";
string userIpAddress = "66.54.123.13";
string ipUrl = ipInfoBaseUrl + userIpAddress;
string ipResponse = IPRequestHelper(ipUrl);
return ipResponse;

我的辅助函数是这样的

public string IPRequestHelper(string url)
{
        string checkURL = url;
        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();

        StreamReader responseStream = new StreamReader(objResponse.GetResponseStream());

        string responseRead = responseStream.ReadToEnd();

        responseRead = responseRead.Replace("\n", String.Empty);
        responseStream.Close();
        responseStream.Dispose();

        return responseRead;
}

我正在使用 Visual Studio Code。我怎样才能摆脱这个错误?

【问题讨论】:

  • 为什么要使用那个命名空间?你想使用什么类?
  • 我正在实施第三方插件以根据 IP 地址获取用户的地理位置。所以在那篇文章中,他使用的是 System.Web。那么在 asp.net 中我应该使用什么来代替 System.Web?
  • 使用HttpClientIHttpClientFactory 代替WebRequest。此外,我在您的代码中看不到任何 System.Web.UI 内容。
  • 你能用HttpClient 转换我的IPRequestHelper,因为我不知道如何使用它。我是 asp.net 核心的新手
  • 查看说明here。应该在 ASP.NET Core 上工作。

标签: c# asp.net-core system.web


【解决方案1】:

该命名空间属于 ASP.NET 网页,不属于 .NET Core,因此您不能在 ASP.NET Core 项目中使用它。

您的选项是 ASP.NET Core razor 页面或 Blazor。

如果您使用的是依赖于旧版 .NET Framework(非核心)的第 3 方库,则可能需要切换回传统的 ASP.NET。

对于 .NET Framework 中的 IP 地址,使用 System.Net.IPAddress

看起来IpGeoLocation.IpGeoLocation 包没有依赖项,但在 JSON.NET 上,所以您应该可以从 ASP.NET Core 使用它。

更新

用以下代码替换该代码:

public async Task<string> GetIpGeoLocationAsync(string url)
{
  using (var cl = new HttpClient())
    return await cl.GetStringAsync(url);
}

另外,请考虑改用IHttpClientFactory

【讨论】:

  • 我正在实施第三方插件以根据ip address 获取用户的geo 位置。所以在那篇文章中,他使用的是System.Web。那么在asp.net 中我应该使用什么来代替System.Web
  • 我已经更新了我的问题,请查看并建议我如何在 asp.net core 2.2 中获取用户 geo 位置,即(城市、国家等)
  • 在我的控制器中的startup.cs 中添加services.AddHttpClient(); 后,我应该导入什么来使用IHttpClientFactor
  • @FahadHassan,假设您在控制器中使用HttpClient,请将IHttpClientFactory 添加到控制器的构造函数参数中并将其存储在私有字段中。而不是创建new HttpClient 使用_HttpClientFactory.CreateClient,参见示例here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2017-05-04
  • 2018-07-15
  • 2011-10-01
  • 2021-08-12
  • 2021-07-27
相关资源
最近更新 更多