为什么 System.Web.HttpUtility.UrlEncode 给出的命名空间名称在 Visual C# 2008 中不存在?
我正在尝试使用 HttpUtility.UrlEncode()
方法对 URL 进行编码,为什么我得到
命名空间“System.Web”中不存在类型或命名空间名称“HttpUtility”(是否缺少程序集引用?)
错误? 我使用的是 Visual C# 2008 Express 版。
我使用的代码很简单:
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();
}
}
}
I'm trying to encode a URL using the HttpUtility.UrlEncode()
method, why am I getting
The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
error ?
I'm using 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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要包含对 System.Web 的引用。在解决方案资源管理器中右键单击您的项目,然后选择“添加引用...”。如果您查看 MSDN 您会看到它包含在 System.Web.dll 程序集中,据我所知,新项目中默认情况下不会引用它。
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 theSystem.Web.dll
assembly, as far as I remember, this is not referenced by default in new projects.以防万一有人偶然发现这一点,正在运行 VS 2010 并且在可用引用中找不到 System.Web...
右键单击该项目并选择属性,如果目标框架设置为“.Net Framework 4 Client”,则更改其为“.Net Framework 4”。
但请注意,这将关闭、重新打开并重建您的项目(此外,如果您有 Web 服务引用,则需要刷新这些项目)
Just in case anyone stumbles across this, is running VS 2010 and cannot find System.Web in the available references...
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".
But beware this will close, reopen and rebuild your project (also if you have a web service references these will need to be refreshed)
对于使用 .NET 4.0 或更高版本的用户,您可以使用 WebUtility.UrlEncode 与客户端配置文件一起使用(不需要 System.Web 程序集引用)。
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).