如何在ASP MVC中编码完整的URL字符串

Dan*_*lba 5 url asp.net-mvc urlencode asp.net-mvc-3 asp.net-mvc-2

我得到一个url字符串,并希望将其转换为合法的http网址:

例如:

"http://one/two/three%four/five#five?six seven" 应该变成 "http://one/two/three%25four/five%23five?six%20seven"

但是,HttpUtility.UrlEncode没有帮助,因为它编码整个字符串(包括合法的"://").

提前致谢

Bal*_*a R 4

看看这是不是你想要的?

   Uri uri = new Uri("http://one/two/three%four/#five?six seven");
   string url = uri.AbsoluteUri + uri.Fragment; 
   // url will be "http://one/two/three%25four/#five?six%20seven#five?six%20seven"
Run Code Online (Sandbox Code Playgroud)