为什么 System.Web.HttpUtility.UrlEncode 给出的命名空间名称在 Visual C# 2008 中不存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1366290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is System.Web.HttpUtility.UrlEncode giving namespace name doesn't exist in Visual C# 2008?
提问by Sathyajith Bhat
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?)
命名空间“System.Web”中不存在类型或命名空间名称“HttpUtility”(您是否缺少程序集引用?)
error ? I'm using Visual C# 2008, Express Edition.
错误 ?我使用的是 Visual C# 2008 速成版。
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();
}
}
}
采纳答案by Cecil Has a Name
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 MSDNyou'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
程序集中,据我所知,在新项目中默认情况下不会引用它。
回答by earcam
Just in case anyone stumbles across this, is running VS 2010 and cannot find System.Web in the available references...
以防万一有人偶然发现,正在运行 VS 2010 并且在可用参考中找不到 System.Web...
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".
右键单击项目并选择属性,如果目标框架设置为“.Net Framework 4 Client”,则将其更改为“.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)
但请注意,这将关闭、重新打开和重建您的项目(如果您有 Web 服务引用,这些将需要刷新)
回答by Adiii
- click on project tab in menu
- click on Add References
- in References window click on Framework and check the System.Web
- 单击菜单中的项目选项卡
- 单击添加引用
- 在 References 窗口中单击 Framework 并检查 System.Web
回答by Lirrik
For people using .NET 4.0 or later, you can use WebUtility.UrlEncodewhich works with client profile (does not require System.Web assembly reference).
对于使用 .NET 4.0 或更高版本的人,您可以使用WebUtility.UrlEncode,它适用于客户端配置文件(不需要 System.Web 程序集引用)。