views:

102

answers:

3

So, i'm a complete noob to C#. I have a url that i need to encode in order to use urlencoded posts.

Issue is, every resource i've found says to use System.Web.HttpServerUtility.UrlEncode. Alternatively, I have seen resources telling me to use System.Web.Util.HttpEncoder().

I have added a reference to system.web, but am getting the "does not exist in system.web namespace" and "does not exist in system.web.util namespace" errors.

Any tips would be nice!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = HttpUtility.UrlEncode("http://stackoverflow.com/");
            MessageBox.Show(url);
        }
     }
}
+3  A: 

You might need to add reference to the System.Web assembly to your project:

alt text

Darin Dimitrov
The screenshot won't really help him, since the "add reference" dialog is completely different in VS2010 (unless he's using the Express version)
Thomas Levesque
Updated screenshot with VS 2010. Not so much different after all. In fact the main difference is invisible in the screenshot. It is in the speed. You no longer need to drink five coffees before seeing this window on previous versions of Visual Studio (thank you Microsoft).
Darin Dimitrov
+2  A: 

Chances are you're using the .NET 4 Client Profile, which doesn't include System.Web.dll - or you just haven't added a reference to it. If you go into the project properties, under "Application" select ".NET Framework 4" as the target framework, rather than ".NET Framework 4 Client Profile". You should then be able to add a reference to the System.Web assembly.

(Note that an assembly is not the same as a namespace. Assembly references are added to a project, whereas you typically "add" namespaces to an individual file with using directives at the top. You've already got the using directive for the System.Web namespace, but that won't help you use HttpServerUtility without a reference to the System.Web assembly.)

Alternatively, depending on your exact requirements, you could use Uri.EscapeDataString or Uri.EscapeUriString. Both of these are available in the client profile. It's slightly annoying that there are so many different ways of doing this - it can be tricky to pick the right one :(

Jon Skeet
That was it! Thanks, after changing from Client Profile to .NET Framework 4, all went smoothly. Thanks again!
Frank Hodgkins
A: 

Hi Frank. I believe you did not add the reference to System.Web.dll.

References and namespace imports are different things. Namespace is a way to group related classes together, so all classes related to collections reside in System.Collections, all Windows Forms stuff is in System.Windows.Forms and you may also want to organize your project to separate business logic, presentation and data access in different namespaces.

However namespaces are purely logical. You can build several libraries that add classes to the same namespaces, and you can also use several namespaces in one project.

You imported System.Web namespace here with the using statement:

using System.Web;

It only means that instead of accessing the class by its full name, System.Web.HttpUtility, you can write just HttpUtility, and the compiler will infer you mean the System.Web one.

However, for Windows applications, the library itself, called System.Web.dll is not referenced by default. This means that none of System.Web classes are even known to the compiler right now.

To add a reference to physical library file, right-click on project References folder, choose 'Add Reference...' and pick System.Web.dll.

And, again, namespaces are just logical groupings, assemblies are physical files on your hard drive.

Good luck, Dan

gaearon