Escaping parts of a URI

  • Thread starter Thread starter Paul E Collins
  • Start date Start date
P

Paul E Collins

I need to escape character strings for use in a URI (as described in RFC
2396).

Uri.EscapeString does what I want, but it is inaccessible due to its
protection level.

How can I achieve this?

P.
 
I need to escape character strings for use in a URI (as
described in RFC 2396).

Uri.EscapeString does what I want, but it is inaccessible due to
its protection level.

How can I achieve this?

Paul,

You can access Uri.EscapeString by creating a Uri subclass:

using System;

namespace Example
{
public class UriEx : Uri
{
// Required because constructors aren't inherited.
public UriEx(
string uriString) : base(uriString) {}

new public static string EscapeString(
string str)
{
return Uri.EscapeString(str);
}
}

public class Test
{
[STAThread]
public static void Main()
{
string input = "http://www.abc.com/page.asp?this is a test";

Console.WriteLine(UriEx.EscapeString(input));
}
}
}


Hope this helps.

Chris.
 
Chris R. Timmons said:
You can access Uri.EscapeString by creating a
Uri subclass:

Thanks; that works well.

It seems a bit odd that Uri.EscapeString isn't a public static member.

P.
 
Back
Top