0

I have asp.net core 2.2 project. I want to use System.Web in my project but when i try to use this in my controller so it says like this

The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) [D:\Projects\analytics\analytics.csproj]

I have project.csproj file like this

<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>

I am using System.Web because i am implementing third party plugin ipinfo to get user's location based on ip address. So in my controller my function is like

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

My helper function is like

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;
}

I am using Visual Studio Code. How can i get rid of this error?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fahad Hassan
  • 781
  • 10
  • 20

1 Answers1

3

That namespace belongs to ASP.NET Web Pages and isn't part of .NET Core, hence you cannot use it in an ASP.NET Core project.

Your options are ASP.NET Core razor pages, or Blazor.

If you're using a 3rd party library that depends on legacy .NET Framework (non-Core), you may want to switch-back and use classic ASP.NET instead.

For IP address in the .NET Framework, use the System.Net.IPAddress class

Looks like the IpGeoLocation.IpGeoLocation package has no dependencies but on JSON.NET, so you should probably be able to use it from ASP.NET Core.

Update

Replace that code with the following:

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

Also, consider using IHttpClientFactory instead.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • I am implementing third party plugin to get user's `geo` location based on `ip address`. So in that article he is using `System.Web`. So in `asp.net` what should i use instead of `System.Web`? – Fahad Hassan Dec 09 '19 at 06:21
  • I have updated my question please see and suggest me that how can i get user `geo` location i.e (city, country, etc) in asp.net core 2.2 – Fahad Hassan Dec 09 '19 at 06:27
  • after adding `services.AddHttpClient();` in `startup.cs` in my controller what should i import to use `IHttpClientFactor` ? – Fahad Hassan Dec 09 '19 at 07:03
  • @FahadHassan, assuming you're using the `HttpClient` in a controller, add `IHttpClientFactory` to your controller's constructor parameters and store it in a private field. Instead of create a `new HttpClient` use `_HttpClientFactory.CreateClient`, see example [here](https://gist.githubusercontent.com/stevejgordon/41b1c75e6f4eca8feca82ec543cd2fac/raw/9ca7a8c21666a81413c60d7b6aa995763fa0758e/ValuesController.cs). – Shimmy Weitzhandler Dec 09 '19 at 07:20