开发者

URL breaking at special character

开发者 https://www.devze.com 2022-12-28 15:23 出处:网络
I\'m having trouble with a URL string in my Web Application.It\'s using a UNC path similar to \\\\houtestmachine\\common\\File1.pdf My problem is when it encounters files that have a #开发者_JAVA技巧

I'm having trouble with a URL string in my Web Application. It's using a UNC path similar to \\houtestmachine\common\File1.pdf My problem is when it encounters files that have a #开发者_JAVA技巧 character. I tried doing a string newstring = originalstring.Replace("#", "%23"); but the # is still there in URL (target of a hyperlink) at runtime in the browser. How can I fix this?


You are converting between file system paths and URLs. The Uri class should fit the bill:

using System;

class Program {
  static void Main(string[] args) {
    var url = new Uri(@"\\houtestmachine\common\F#ile1.pdf");
    Console.WriteLine(url.AbsoluteUri);
    var back = url.LocalPath;
    Console.WriteLine(back);
    Console.ReadLine();
  }
}

Output:

file://houtestmachine/common/F%23ile1.pdf
\\houtestmachine\common\F#ile1.pdf


Have you tried HttpUtility.UrlEncode()?


Use symbol @ befor string. For example

string st = @"your#path"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号