简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM