简体   繁体   English

从Web API访问虚拟目录

[英]Accessing a Virtual Directory from a Web API

I have a folder of files (form templates) that need to be accessed from both a .NET Web API application and a separate .NET MVC application. 我有一个文件夹(表单模板),需要从.NET Web API应用程序和单独的.NET MVC应用程序访问。 The folder for these form templates is outside of the websites. 这些表单模板的文件夹位于网站之外。 Ideally, I'd like to be able to store the path in the Web.Config files so that the applications can be easily tested in a local environment. 理想情况下,我希望能够将路径存储在Web.Config文件中,以便可以在本地环境中轻松测试应用程序。

I tried setting up virtual directories on the hosted site but couldn't figure out how to reference them in the Web API. 我尝试在托管网站上设置虚拟目录,但无法弄清楚如何在Web API中引用它们。 I tried several means of referencing the Virtual Directory that did not work. 我尝试了几种引用虚拟目录的方法。 Most posts suggested trying Server.MapPath("MyVirtualDirectory") but that returns "....\\wwwroot\\MyApiController\\Action\\MyVirtualDirectory", not the physical path of the virtual directory. 大多数帖子建议尝试Server.MapPath(“MyVirtualDirectory”)但返回“.... \\ wwwroot \\ MyApiController \\ Action \\ MyVirtualDirectory”,而不是虚拟目录的物理路径。

I removed the Virtual Directory and attempted to "navigate" to the correct path but was blocked by "Cannot use a leading .. to exit above the top directory". 我删除了虚拟目录,并尝试“导航”到正确的路径,但被“无法使用前导..退出顶层目录”阻止。

So what is the correct way to access a resource using a virtual directory in .NET Web API application? 那么在.NET Web API应用程序中使用虚拟目录访问资源的正确方法是什么? Is the same method going to work for the .NET MVC application? 同样的方法是否适用于.NET MVC应用程序?

You need to use HostingEnvironment , like: 您需要使用HostingEnvironment ,例如:

public static string MapPath(string path){
    string result;

    result = HostingEnvironment.MapPath(path);

    return result;
}

Additionally HostingEnvironment provides features like ApplicationPhysicalPath : 此外, HostingEnvironment还提供ApplicationPhysicalPath功能:

result = HostingEnvironment.ApplicationPhysicalPath + "App_Data\\somefile.xml";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 宁静的Web API无法从移动设备访问 - Restful web api not accessing from mobile devices 从虚拟目录或Web根以全局方式引用资源? - Referencing resources in a global way either from a virtual directory or the web root? 使用HttpClient访问作为虚拟目录运行的Web服务 - Accessing a webservice running as a virtual directory, with HttpClient SharePoint 2007虚拟目录中的MVC4(Web API)控制器错误:访问被拒绝 - MVC4 (Web API) controller in SharePoint 2007 Virtual Directory Error: Access is Denied jQuery获取虚拟目录中的位置以及负载均衡器后面的位置(MVC Web API) - jquery GET to location in virtual directory AND behind a load balancer (mvc web api) 在ASP.NET MVC应用程序下将ASP.NET Web API应用程序配置为虚拟目录 - Configure ASP.NET Web API application as a Virtual Directory under ASP.NET MVC application Orchard Web应用程序内的虚拟目录 - Virtual directory inside Orchard web application 在同一个 azure web 应用程序上创建虚拟目录 - Create virtual directory on same azure web app 使用虚拟目录的Api调用MVC网址失败 - Api calls to MVC urls with Virtual Directory fails 访问远程服务器IIS7上的虚拟目录时出现运行时错误 - Runtime Error when accessing virtual directory on remote server IIS7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM