In my blog, I store URIs on entities to allow them be customised (and friendly). Originally, they could contain spaces (eg. "/tags/ASP.NET MVC"), but the W3C validation says spaces are not valid.
The System.Uri
class takes spaces, and seems to encode them as I want (eg. /tags/ASP.NET MVC
becomes /tags/ASP.NET%20MVC
), but I don't want to create a Uri
just to throw it away, this feels dirty!
Note: None of Html.Encode
, Html.AttributeEncode
and Url.Encode
will encode "/tags/ASP.NET MVC"
to "/tags/ASP.NET%20MVC"
.
Edit: I edited the DataType
part out of my question as it turns out DataType does not directly provide any validation, and there's no built-in URI validation. I found some extra validators at dataannotationsextensions.org but it only supports absolute URIs and it looks like spaces my be valid there too.
/tags/Tup Peny
and make sure it's encoded for the context when emitting it (in your case; URL encoding it for URL use). Does that make sense? :) – Burdine