I think you are attacking it from the wrong angle by trying to encode all posted data.
Note that a "<
" could also come from other outside sources, like a database field, a configuration, a file, a feed and so on.
Furthermore, "<
" is not inherently dangerous. It's only dangerous in a specific context: when writing strings that haven't been encoded to HTML output (because of XSS).
In other contexts different sub-strings are dangerous, for example, if you write an user-provided URL into a link, the sub-string "javascript:
" may be dangerous. The single quote character on the other hand is dangerous when interpolating strings in SQL queries, but perfectly safe if it is a part of a name submitted from a form or read from a database field.
The bottom line is: you can't filter random input for dangerous characters, because any character may be dangerous under the right circumstances. You should encode at the point where some specific characters may become dangerous because they cross into a different sub-language where they have special meaning. When you write a string to HTML, you should encode characters that have special meaning in HTML, using Server.HtmlEncode. If you pass a string to a dynamic SQL statement, you should encode different characters (or better, let the framework do it for you by using prepared statements or the like)..
When you are sure you HTML-encode everywhere you pass strings to HTML, then set ValidateRequest="false"
in the <%@ Page ... %>
directive in your .aspx
file(s).
In .NET 4 you may need to do a little more. Sometimes it's necessary to also add <httpRuntime requestValidationMode="2.0" />
to web.config (reference).
Uri
has a constructor that should do this for you: new Uri(Uri baseUri, string relativeUri)
Here's an example:
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Note from editor: Beware, this method does not work as expected. It can cut part of baseUri in some cases. See comments and other answers.
Best Answer
As there have been no answers to this I am going to answer my own question.
The answer is that this does not appear to be possible- there is no way to set the culture for a validator directly.
The way in which I was able to accomplish my goals of having the server-side code running always in an english culture but then causing the validators to work in the browser culture was to set the thread currentculture at the end of Page_PreRender. Therefore up to this point the code runs in english culture, but we set the culture in time for it to be in effect for when the asp.net runtime uses it to set up the validators.
This works fine for the cient-side action of the validators, in my case allowing users on a french browser to enter numbers in a french format. But there is a further gotcha- if you have code to validate server side on submit/ postback, this validation will now fail- it appears that on postback the original culture settings of the validator are not retained, they use the culture in place at the time they are validated server-side: of course the french numbers are not then viewed as valid, and the validation fails.
I hope this helps anyone else who encounters the same issue