tags:

views:

1866

answers:

4

Is there any function that converts an escaped Url string to its unescaped form? System.Web.HttpUtility.UrlDecode() can do that job but I don't want to add a reference to System.Web.dll. Since my app is not a web application, I don't want to add a dependency for only using a function in an assembly.

UPDATE: Check Rick Strahl's blog post about the same issue.

A: 

The Microsoft ACE team have an extended (and better) version of decode, in the Anti-XSS library. However I'm not sure if it just passes through.

(I don't see why you're that worried about the dependency on System.web.dll to be honest)

blowdart
The reason is that I don't want to load a rather large DLL into my memory space for the sake of only one method call. It would be good if someone points an implementation (probably uses Regex) that I can copy/paste into my project.
huseyint
A regex solution is not going to be enough; decoding is *hard*.If you're really worried then, *cough*, reflector.Have you actually looked at the memory cost of loading the assembly? It's probably not as large as you think.
blowdart
Note that different framework versions (esp. "client profile") are a valid reason to avoid it - but not worth getting *too* excited over it ;-p
Marc Gravell
Actually, the AntiXss library does not provide the decoding methods, only encoding.
hmemcpy
+2  A: 

You already have a HUGE dependency on the .NET framework, CLR etal. So, in fact, you already have an indirect dependency on System.Web.DLL; your application CAN NOT RUN without its presence on the local machine.

And you're worried about memory? Do you have memory issues? If you have memory issues so extreme you can't load a couple KBs of DLL into your app's memory, then why are you coding .NET? Or are you just prematurely optimizing?

So don't worry about it.

Will
Your assertion is incorrect; "client profile", "cf" and "silverlight" will all lack System.Web.dll; "client profile" is a version of regular .NET, so is the most striking.
Marc Gravell
@marc iirc when this was answered the client profiles weren't released/commonly known about. Second, he doesn't say what kind of app, and in fact from the way he asks the question its obvious that it IS an option to reference system.web.dll. Otherwise the question is moot. So my assertation is most likely correct. Nyah.
Will
+2  A: 

Re not loading System.Web.dll - as others have noted, it isn't worth getting excited unless you know that you need to deal with clients that might not have it ("client profile", "compact framework", "micro framework", "silverlight").

Re space; it won't be a lot really; note that .NET assemblies are JITted on a method-by-method basis, so there won't be any significant overhead just from using a few methods.

The real issue (IMO) is your level of confidence that the client has System.Web.dll; if you are happy that they are using the full framework, then just go for it.

Marc Gravell
+4  A: 

EDIT: Use the static method Uri.UnescapeDataString() to decode your URLs:

Encoded: http%3a%2f%2fwww.google.com%2fsearch%3fhl%3den%26q%3dsomething%20%2323%26btnG%3dGoogle%2bSearch%26aq%3df%26oq%3d

Decoded: http://www.google.com/search?hl=en&q=something #23&btnG=Google+Search&aq=f&oq=

hmemcpy
This is what I was looking for, thanks!
huseyint