views:

1278

answers:

1

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();

        }

    }
}
+7  A: 

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.

Cecil Has a Name
doesn't "using System.web" add the reference automatically ?
Sathya
Have you checked that it is referenced?
Cecil Has a Name
Indeed it was not referenced, added the reference and works.
Sathya
@Sathya: Adding `using` directives has nothing to do with references, it merely gives you the possibility to use types from that namspace in the code file without specifying the full type name (so you can write `HttpUtility.UrlEncode` instead of `System.Web.HttpUtility.UrlEncode`).
Fredrik Mörk
@Fredrik I see.
Sathya