Uri.EscapeDataString在NUnit测试和asp.net webservice中的行为不同? [英] Uri.EscapeDataString behaves different in NUnit test vs. asp.net webservice?

查看:141
本文介绍了Uri.EscapeDataString在NUnit测试和asp.net webservice中的行为不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Uri .EncodeDataString 在URL上发送查询字符串。原来的字符串是:照片($ select = Name,Id)
在我的asp.net web服务运行在VS2015调试器中,Uri.EncodeDataString将返回: 照片(%24select%3DName%2CId)
但是,在VS2015中的Resharper测试运行器下运行的NUnit测试中,它返回:照片%28%24select%3DName%2CId%29 。注意括号中编码的区别。



为什么括号在单元测试中编码但不在Web服务中编码?括号中不应该在Web服务中编码吗?我正在.Net 4.6下运行,所以我不应该受到StackOverflow其他地方的RFC 3986问题的影响(这里 here here

解决方案

最后,我确实受到RFC 3986问题的影响。 / p>

事实证明,区别是.Net 4.5的怪异模式。当在asp.net应用程序中运行时,除非明确设置,否则.Net将尝试维护与以前版本的应用程序兼容性。



我进入框架代码,并发现一个奇怪的设置在UriParser。 Google搜索引导我进入这篇文章关于web.config中的< httpruntime> 元素。接近尾声,它说:


如果没有< httpRuntime targetFramework> 属性存在于
Web.config中,我们假设应用程序想要4.0怪癖
行为。


一旦我将$ code> targetFramework =4.5添加到我的web.config中,Uri.EscapeDataString的行为就像我预期的那样。


I am using Uri.EncodeDataString to send a query string on a URL. The original string is: Photo($select=Name,Id) In my asp.net web service running in the VS2015 debugger, Uri.EncodeDataString will return: Photo(%24select%3DName%2CId) However, in an NUnit test running under the Resharper test runner in VS2015, it returns: Photo%28%24select%3DName%2CId%29. Notice the difference in the encoding of the parentheses.

Why are the parentheses encoded in the unit test but not in the web service? Shouldn't the parentheses be encoded in the web service? I'm running under .Net 4.6, so I shouldn't be affected by the RFC 3986 issue discusssed elsewhere on StackOverflow (here, here, here and a plethora of other places around the net).

解决方案

In the end, I was indeed affected by the RFC 3986 issue.

It turns out the difference is a "quirks" mode of .Net 4.5. When running in an asp.net application, unless explicitly set, .Net will attempt to maintain application compatibility with previous versions.

I stepped into framework code and found a quirk setting in UriParser. Googling led me to this article about the <httpruntime> element in web.config. Near the end, it says:

"If there is no <httpRuntime targetFramework> attribute present in Web.config, we assume that the application wanted 4.0 quirks behavior."

Once I added targetFramework="4.5" to my web.config, Uri.EscapeDataString behaved as I expected.

这篇关于Uri.EscapeDataString在NUnit测试和asp.net webservice中的行为不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆