Why can't I find or use UrlEncode in Visual Studio 2010?

孤街醉人 提交于 2019-12-03 23:28:05

By default, new projects in Visual Studio 2010 target the .NET Framework 4.0 Client Profile, which does not include the System.Web assembly.

You can change the version of the Framework that your project targets in your project's Properties. Under the "Application" tab, select ".NET Framework 4.0" from the combobox labeled "Target framework".

Then, make sure that you have added a reference to System.Web using the "Add Reference" dialog.

Finally, add a using directive to the top of your class for the System.Web namespace:

using System.Web;


You'll find the various overloads of the UrlEncode method in the HttpUtility class. Sample code:

HttpUtility.UrlEncode("http://www.google.com/");

In .Net 4.5 you can (should?, 'please use' says a Katana comment) use the System.Net.WebUtility.UrlEncode method.

Tim S.

It can't be named differently since Visual Studio doesn't supply the class or method names, the .NET framework does.

All I can tell you is that the System.Web.HttpUtility AND System.Web.HttpServerUtility classes contain a method called UrlEncode(string).

If your project target ".NET Framework X Client Profile",you cannot not use "System.Web",but you can use "Uri.EscapeUriString | Uri.UnEscapeUriString" instead.

rdouglass

Yes, adding the reference was my answer. But be sure you double check the project, that it is in, if you have more than 1 project in your solution. I had a solution with 3 projects. System.Web was added to 2 projects but not the 3rd project.

I spent an hour trying to figure out why I couldn't use HttpUtility since it was a Reference in the main project. But I didn't check the sub-projects of the Solution.

Hope it helps someone.

Tim S.

Because you only see AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel, I strongly suspect (like the other guys) that you're missing a reference.

The best you can do is start a new project, because it's pretty complicated to add/remove references without ruining your entire project.

How to: Add or Remove References in Visual Studio (MSDN) shows how to add/remove references. In your case, you should check/add the System.Web reference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!