为什么System.Web.HttpUtility。命名空间名称的UrlEncode在Visual c# 2008中不存在吗?

时间:2022-01-17 16:44:51

I'm trying to encode a URL using the HttpUtility.UrlEncode() method, why am I getting

我正在尝试使用HttpUtility.UrlEncode()方法对URL进行编码,为什么会这样呢

The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

类型或名称空间名称“HttpUtility”在名称空间的系统中不存在。Web'(您是否缺少汇编引用?)

error ? I'm using Visual C# 2008, Express Edition.

错误呢?我正在使用Visual c# 2008, Express Edition。

The code I'm using is simplistic:

我使用的代码过于简单:

using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
namespace Lincr
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void cmdShorten_Click(object sender, EventArgs e)
        {
            WebRequest wrURL;
            Stream objStream;
            wrURL = WebRequest.Create("http://lin.cr?l=" + System.Web.HttpUtility.UrlEncode(txtURL.Text) + "&mode=api&full=1");
            objStream = wrURL.GetResponse().GetResponseStream();
            StreamReader objSReader = new StreamReader(objStream);
            textBox1.Text = objSReader.ReadToEnd().ToString();

        }

    }
}

4 个解决方案

#1


29  

You need to include a reference to System.Web. Right-click your project in the Solution Explorer and choose Add Reference... . If you take a look at MSDN you'll see it's contained in the System.Web.dll assembly, as far as I remember, this is not referenced by default in new projects.

您需要包含对System.Web的引用。在解决方案资源管理器中右键单击项目并选择Add Reference…。如果你看一下MSDN,你会发现它包含在System.Web中。就我所知,在新项目中默认不引用dll程序集。

#2


29  

Just in case anyone stumbles across this, is running VS 2010 and cannot find System.Web in the available references...

以防有人遇到这种情况,运行VS 2010并且找不到系统。在可用的参考资料中。

Right click on the project and select Properties, if the Target Framework is set to ".Net Framework 4 Client" then change it to ".Net Framework 4".

如果目标框架被设置为“”,右键单击项目并选择Properties。Net Framework 4客户端“然后将其更改为”。净框架4”。

But beware this will close, reopen and rebuild your project (also if you have a web service references these will need to be refreshed)

但是要注意,这将关闭、重新打开并重新构建项目(如果您有web服务引用,则需要刷新它们)

#3


3  

For people using .NET 4.0 or later, you can use WebUtility.UrlEncode which works with client profile (does not require System.Web assembly reference).

对于使用。net 4.0或更高版本的用户,您可以使用WebUtility。使用客户端概要文件的UrlEncode(不需要系统)。Web组件引用)。

#4


0  

  1. click on project tab in menu
  2. 单击菜单中的项目选项卡
  3. click on Add References
  4. 点击添加引用
  5. in References window click on Framework and check the System.Web
  6. 在引用窗口中单击框架并检查System.Web。

#1


29  

You need to include a reference to System.Web. Right-click your project in the Solution Explorer and choose Add Reference... . If you take a look at MSDN you'll see it's contained in the System.Web.dll assembly, as far as I remember, this is not referenced by default in new projects.

您需要包含对System.Web的引用。在解决方案资源管理器中右键单击项目并选择Add Reference…。如果你看一下MSDN,你会发现它包含在System.Web中。就我所知,在新项目中默认不引用dll程序集。

#2


29  

Just in case anyone stumbles across this, is running VS 2010 and cannot find System.Web in the available references...

以防有人遇到这种情况,运行VS 2010并且找不到系统。在可用的参考资料中。

Right click on the project and select Properties, if the Target Framework is set to ".Net Framework 4 Client" then change it to ".Net Framework 4".

如果目标框架被设置为“”,右键单击项目并选择Properties。Net Framework 4客户端“然后将其更改为”。净框架4”。

But beware this will close, reopen and rebuild your project (also if you have a web service references these will need to be refreshed)

但是要注意,这将关闭、重新打开并重新构建项目(如果您有web服务引用,则需要刷新它们)

#3


3  

For people using .NET 4.0 or later, you can use WebUtility.UrlEncode which works with client profile (does not require System.Web assembly reference).

对于使用。net 4.0或更高版本的用户,您可以使用WebUtility。使用客户端概要文件的UrlEncode(不需要系统)。Web组件引用)。

#4


0  

  1. click on project tab in menu
  2. 单击菜单中的项目选项卡
  3. click on Add References
  4. 点击添加引用
  5. in References window click on Framework and check the System.Web
  6. 在引用窗口中单击框架并检查System.Web。