为什么我在 Visual Studio 2010 中找不到或使用 UrlEncode?

发布于 2024-10-17 07:36:39 字数 302 浏览 3 评论 0原文

我有一个字符串,我想将其编码为标准 URL 格式。根据我的发现,我应该能够通过 httpUtility.urlEncode 方法来执行此操作,但我似乎没有可用的方法。

我已添加对 System.WebSystem.Net 的“using”引用,但无济于事。我还看到了对 server.urlEncode 以及其他变体的其他引用,但我在任何地方都没有看到该方法。

我在 Visual Studio 2010 中使用最新版本的 C#。此版本中的方法调用是否有所不同,隐藏在其他地方,还是我完全偏离了基础?

I have a string that I'd like to encode into the standard URL format. From what I've found, I should be able to do this via the httpUtility.urlEncode method, but I don't seem to have that available.

I've added "using" references to both System.Web and System.Net to no avail. I've also seen other references to server.urlEncode amongst other variants, but I don't see the method anywhere.

I'm using the latest version of C# in Visual Studio 2010. Is the method called something different in this version, hidden somewhere else, or am I completely off base?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花开半夏魅人心 2024-10-24 07:36:39

默认情况下,Visual Studio 2010 中的新项目以 .NET Framework 4.0 Client Profile 为目标,其中不包含 System.Web 程序集。

您可以在项目属性中更改项目所针对的框架版本。在“应用程序”选项卡下,从标有“目标框架”的组合框中选择“.NET Framework 4.0”。

然后,确保您已使用“添加引用”对话框添加对 System.Web 的引用。

最后,向System.Web 命名空间的类顶部:

using System.Web;

您会发现 UrlEncode 方法的各种重载< /a> 在 HttpUtility 类中。示例代码:

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

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/");
演出会有结束 2024-10-24 07:36:39

在 .Net 4.5 中,您可以(应该?,Katana 注释中说“请使用”)使用 System.Net.WebUtility.UrlEncode 方法。

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

网白 2024-10-24 07:36:39

它不能以不同的方式命名,因为 Visual Studio 不提供类或方法名称,而 .NET 框架提供。

我只能告诉您,System.Web.HttpUtility 和 System.Web.HttpServerUtility 类包含一个名为 UrlEncode(string) 的方法。

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).

挽你眉间 2024-10-24 07:36:39

如果您的项目目标为“.NET Framework X Client Profile”,则不能不使用“System.Web”,但可以使用“Uri.EscapeUriString | Uri.UnEscapeUriString”代替。

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

童话 2024-10-24 07:36:39

是的,添加参考文献就是我的答案。但如果您的解决方案中有超过 1 个项目,请务必仔细检查该项目是否存在。我有一个包含 3 个项目的解决方案。 System.Web 已添加到 2 个项目,但未添加到第 3 个项目。

我花了一个小时试图弄清楚为什么我不能使用 HttpUtility,因为它是主项目中的参考。但我没有检查解决方案的子项目。

希望它能帮助某人。

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.

电影里的梦 2024-10-24 07:36:39

因为您只看到 AspNetHostingPermissionAspNetHostingPermissionAttributeAspNetHostingPermissionLevel,所以我强烈怀疑(像其他人一样)您缺少引用。

您能做的最好的事情就是开始一个新项目,因为在不破坏整个项目的情况下添加/删除引用非常复杂。

如何:添加或删除引用Visual Studio (MSDN) 展示了如何添加/删除引用。对于您的情况,您应该检查/添加 System.Web 引用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文