1

Ive got a working webmail and i want to send an embedded image.

 LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"..\Content\img\GladSmiley.png", "image/png");

when i use the code above it will search for the mappath to the image witch is located in the \projectname\content\img folder. But the mappath method is looking for the img in \projectname\home\content\img so by some reason it adds the home-folder to the mappath :S Is there a way to go around this problem? or am i doing something wrong?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78

1 Answers1

1

Try this instead:

LinkedResource imagelink = new LinkedResource(HostingEnvironment.MapPath("~/Content/img/GladSmiley.png"), "image/png");

The relevant bit is that you find the directory/file like this:

HostingEnvironment.MapPath("~/Content/img/GladSmiley.png")

You can also use Server.MapPath with exactly the same syntax if you want, but you need an HttpContext to do that (which you probably have, but for the sake of making it always work, use HostingEnvironment.MapPath).

HostingEnvironment.MapPath("~/Content/img/GladSmiley.png") // works everywhere
Server.MapPath("~/Content/img/GladSmiley.png") // needs HttpContext

See this discussion for more information if you're interested: What is the difference between Server.MapPath and HostingEnvironment.MapPath?

Community
  • 1
  • 1
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • That's weird (I guess I made a typo or misremembered something). Do you get the same error if you use `Server.MapPath` or `HostingEnvironment.MapPath`? What is the error? – Tom Chantler Sep 18 '13 at 08:07
  • I did make a typo. Have changed it – Tom Chantler Sep 18 '13 at 08:08
  • Yes, the same error. "\Content\img\GladSmiley.png" this gives me the error "bad compile constant error" when i google it i get something about escaping and using double \ like "\\Content\\img\\GladSmiley.png" but thats doesn't work either – Daniel Gustafsson Sep 18 '13 at 08:11
  • Okej, now i get "possible 'null' assignment to entity marked with 'notnull' attribute" – Daniel Gustafsson Sep 18 '13 at 08:13
  • Are you using forward slashes `/` like I have in my example? They don't need escaping. In the MapPath bit you want `"~/Content/img/GladSmiley.png"` – Tom Chantler Sep 18 '13 at 08:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37575/discussion-between-dommer-and-daniel-gustafsson) – Tom Chantler Sep 18 '13 at 08:27