views:

34

answers:

4

I am working on a mailSender Class. I can't use the Server.MapPath method from the System.Web.UI Namespace. What reference should i set to make this work? I have referenced to the System / System.Web / System.Web.UI.

This is the msdn i have checked.

A: 

Have you tried this

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx

drorhan
have tried it but it won't find the Server. "The name server does not exist in the current context".
Younes
have you added the reference
drorhan
I am referencing to System.Web -> C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll Shouldn't it be the 3.5 framework???
Younes
Are you in ASP.NET environment or your code is running as regular or service application?
Andrey Shvydky
I am in ASP.NET environment yes. I have referenced to System.Web aswell. If i check the propperties of the reference i see it's using one from the 2.0 framework, is that okay?
Younes
have you added in the solution reference directory and at the top code unit with using ?
drorhan
Are your cs file included in the project ?
drorhan
A: 

Server.MapPath is in the System.Web namespace:

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx

Robin M
I have done: using System.Web yes.
Younes
+1  A: 

Please read TaylorMichaelL answer on this page: The name 'Server' does not exist in the current context. This should help you.

A better approach though is that you add the target folder as a method argument and create the path from that. That way, the class will be usable in non-web contexts as well.

Andrey Shvydky
Thanks alot, i now know that i can't use the Server object within my cs file ;). I wil have to ensure my class method will be able to accept a string with the mappath instead of trying to GET the mappath there :). Thx alot!
Younes
can you show your code ?
drorhan
+1  A: 

For people that will encounter this problem i will answer my own question so they don't have to follow links to other websites. If you want to use the Server.Mappath, you won't be able to do this when using a DAL or BL. You can gain access to these objects (assuming they are available at the time given that they are contextual) by using the HttpContext.Current property. This property gives you the current context. However you are now trying your DAL/BL to ASP.NET. So what you could do to fix this issue is getting the Server.Mappath on a place where it is contextual, like in your Code Behind.

GL!

Younes