【问题标题】:System.Net.Http missing?System.Net.Http 丢失?
【发布时间】:2014-10-09 07:33:03
【问题描述】:

我正在尝试运行 Test.aspx:

<%@ Page language="c#" EnableViewState="true" ContentType="text/html" Async="true" %>

<script language="C#" runat="server">

    void Page_Load(Object Src, EventArgs E )
    {
        RegisterAsyncTask(new PageAsyncTask(BindData));
    }

    private async System.Threading.Tasks.Task BindData()
    {
        Response.Write("Hello?<br /><br />");

        using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
        {
            Response.Write(await httpClient.GetStringAsync("http://www.google.com"));
        }

        Response.Write("<br /><br />Is this thing on?<br /><br />");
    }

</script>

并收到此错误:

Test.aspx(14): error CS0234: The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

System.Net.Http.dll 程序集在

C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies

在 IIS 管理器中,基本设置的应用程序池是 ASP.NET v4.0(集成)。有人遇到过这种情况吗?

更新:我安装了 .Net 4.5.2 并添加了以下 web.config

<configuration>
  <system.web>
    <httpRuntime targetFramework="4.5" />
   <compilation>
      <assemblies>
         <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </assemblies>
   </compilation>
  </system.web>

</configuration>

它成功了。

【问题讨论】:

  • 确保您在 .NET 4.5 中引用 System.Net.Http.dll
  • 这是一个独立的 .aspx 页面,没有代码隐藏。它不是解决方案或项目的一部分,也不在 Visual Studio 中。添加 没有帮助。
  • 1) 如果您没有正确的框架版本,将无济于事。 是否您安装了 4.5? 2) Import 用于命名空间。您需要使用Assembly 来添加对dll 的引用。无论您使用什么 IDE,都需要对程序集的引用。 VS 只是让添加它们更容易
  • 要解决这个问题,我必须添加 到 web.config

标签: asp.net async-await dotnet-httpclient


【解决方案1】:

为了解决这个问题,我必须添加

<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

 <httpRuntime targetFramework="4.5.2" />

到 web.config

【讨论】:

    【解决方案2】:

    我已经尝试过这个强烈建议的解决方案(我在 SO 中的许多类似问题中发现这是公认的答案)

    Web.Config 上写下这个并重建解决方案

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
           <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
          </assemblies>
        </compilation>
    </system.web>
    

    但不幸的是,我没有运气。最后,我找到了一个对我有用的解决方案并拯救了我的一天:)

    1. 右击References文件夹>Add Reference...
    2. 在窗口左侧展开程序集并选择框架。
    3. 在程序集列表中滚动到并选择System.Net.Http。 确保选中 System.Net.Http 旁边的框,然后单击 OK
    4. 重建项目。

    【讨论】:

      【解决方案3】:

      就我而言,问题是在升级到 .NET Framework 4.7.2 后开始出现的。我发现不再推荐 System.Net.Http 的 Nuget 包。以下是解决方法:

      【讨论】:

        【解决方案4】:

        要解决我的问题,只需在 webconfig 中添加即可。

        <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
        

        <httpRuntime targetFramework="4.5" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-12
          • 1970-01-01
          • 1970-01-01
          • 2017-02-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多