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);
}
}
}
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 :(
You might need to add reference to the System.Web assembly to your project:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With