为什么我在 Visual Studio 2010 中找不到或使用 UrlEncode?
我有一个字符串,我想将其编码为标准 URL 格式。根据我的发现,我应该能够通过 httpUtility.urlEncode 方法来执行此操作,但我似乎没有可用的方法。
我已添加对 System.Web
和 System.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
默认情况下,Visual Studio 2010 中的新项目以 .NET Framework 4.0 Client Profile 为目标,其中不包含
System.Web
程序集。您可以在项目属性中更改项目所针对的框架版本。在“应用程序”选项卡下,从标有“目标框架”的组合框中选择“.NET Framework 4.0”。
然后,确保您已使用“添加引用”对话框添加对
System.Web
的引用。最后,向
System.Web
命名空间的类顶部:您会发现
UrlEncode
方法的各种重载< /a> 在HttpUtility
类中。示例代码: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 theSystem.Web
namespace:You'll find the various overloads of the
UrlEncode
method in theHttpUtility
class. Sample code:在 .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.
它不能以不同的方式命名,因为 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)
.如果您的项目目标为“.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.
是的,添加参考文献就是我的答案。但如果您的解决方案中有超过 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.
因为您只看到
AspNetHostingPermission
、AspNetHostingPermissionAttribute
和AspNetHostingPermissionLevel
,所以我强烈怀疑(像其他人一样)您缺少引用。您能做的最好的事情就是开始一个新项目,因为在不破坏整个项目的情况下添加/删除引用非常复杂。
如何:添加或删除引用Visual Studio (MSDN) 展示了如何添加/删除引用。对于您的情况,您应该检查/添加
System.Web
引用。Because you only see
AspNetHostingPermission
,AspNetHostingPermissionAttribute
, andAspNetHostingPermissionLevel
, 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.