微软的Web API:你如何做一个使用Server.Mappath? [英] Microsoft Web API: How do you do a Server.MapPath?

查看:202
本文介绍了微软的Web API:你如何做一个使用Server.Mappath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于微软的Web API是不是MVC,你不能做这样的事情:

  VAR一个= Request.MapPath(〜);

和本

 变种B =使用Server.Mappath(〜);

,因为这些是System.Web命名空间之下,而不是System.Web.Http命名空间。

那么,你如何找出在网页API相对于服务器的路径?我以前做的MVC是这样的:

  VAR MYFILE = Request.MapPath(〜/内容/图片/+文件名);

这将使我的绝对路径在磁盘上:

 C:\\的Inetpub \\ wwwroot的\\ myWebFolder \\内容\\图片\\ mypic.jpg


解决方案

您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath.aspx\">HostingEnvironment.MapPath在任何情况下,其中的System.Web 对象,如 HttpContext.Current 不可用(如也从一个静态方法)。

  VAR mappedPath = System.Web.Hosting.HostingEnvironment.MapPath(〜/ SomePath);

又见<一个href=\"http://stackoverflow.com/questions/944219/what-is-the-difference-between-server-mappath-and-hostingenvironment-mappath\">What是使用Server.Mappath之间的差异,HostingEnvironment.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:\inetpub\wwwroot\myWebFolder\Content\pics\mypic.jpg"

解决方案

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?

这篇关于微软的Web API:你如何做一个使用Server.Mappath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆