Search code examples
c#asp.netwebmatrixusing-directives

What using directive do I use for Server.MapPath in a separate .cs file in WebMatrix?


I am trying to use Server.MapPath() in a separate .cs file in WebMatrix.

I can get the method going fine by using HttpContext.Current.Server.MapPath("~/SomeDirectory/someFile.txt")

but do I really have to type HttpContext.Current every time?

Isn't there any using directive to shorten my typing to simply Server.MapPath like it is when coding in a .cshtml file?

I have looked here, but none of the answers worked for me. Probably because the answers therein don't target an asp.net-webpages, WebMatrix environment.

http://www.sitepoint.com/forums/showthread.php?167802-Server-MapPath-in-C-class

Server.MapPath()

Also, if there is a kind of cheat sheet to help me find C#.net using directives, considering my environment, letting me know where I can find it is definitely an acceptable answer.


Solution

  • You can assign the Server property to a variable to save a bit of typing:

    var server = HttpContext.Current.Server;
    var file = server.MapPath("~/SomeDirectory/Somefile");
    

    To answer the question posed in the title of your post - the using directive you need in the class file is System.Web:

    using System.Web;
    

    I don't know of any "cheat sheet" as such. Experience and a growing familiarity with MSDN help, as does Resharper if you can afford it and have the full version of Visual Studio.