Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

c# - Uri.EscapeDataString behaves different in NUnit test vs. asp.net webservice?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...