Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

asp.net web api - Microsoft Web API: How do you do a Server.MapPath?

Since Microsoft Web API isn't MVC, you cannot do something like this:

var a = Request.MapPath("~");

nor this

var b = Server.MapPath("~");

because these are under the System.Web namespace, not the System.Web.Http namespace.

So how do you figure out the relative server path in Web API ?
I used to do something like this in MVC:

var myFile = Request.MapPath("~/Content/pics/" + filename);

Which would give me the absolute path on disk:

"C:inetpubwwwrootmyWebFolderContentpicsmypic.jpg"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use HostingEnvironment.MapPath in any context where System.Web objects like HttpContext.Current are not available (e.g also from a static method).

var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/SomePath");

See also What is the difference between Server.MapPath and HostingEnvironment.MapPath?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...