0

See the code below. I'm not sure why the two extra // are appearing before the file extension or how to best handle?

string avatarFilePath = "~/_images/avatar/";
string userId = 53da95a1-cc48-42d0-9a00-167f47ce5933";
string avatarFileExt = ".png";

string path = Path.Combine(avatarFilePath, userId, avatarFileExt);

//value of 'path' is: "~/_images/avatar/53da95a1-cc48-42d0-9a00-167f47ce5933\\.png" <-- note two slashes before extension
PixelPaul
  • 2,609
  • 4
  • 39
  • 70

1 Answers1

2

Path.Combine is not intended to create file names or build urls. You should build the file name first, then use Uri to build your url. Something like

string avatarFilePath = "~/_images/avatar/";
string userId = 53da95a1-cc48-42d0-9a00-167f47ce5933";
string avatarFileExt = ".png";
string fileName = String.Format("{0}{1}", userId, avatarFileExt);
Uri uri = new Uri(avatarFilePath, fileName);
string url = uri.ToString();

if you want a physical file path you could go with

Server.MapPath(url);
Miniver Cheevy
  • 1,667
  • 2
  • 14
  • 20