c# - 如何从 Asp.net 中的 web api 访问客户端项目目录

标签 c# asp.net angularjs asp.net-web-api2

我的 Asp.net 项目是多层项目,如 DALDATAWebApi .... 等。

我的UI层是html5,angularjs等组合

我的 UI 层中没有服务器端代码。

我的 UI 层通过 rest api 与 webapi 层通信。

现在我想要我的 UI 层的文件夹列表。我怎样才能做到这一点 ?因为客户端脚本无权执行此操作。所以,只剩下 web api 层。我是否可以从我的 web api 层获取 UI 层的目录列表?

如果是,我该怎么做?如果否,还有什么选择以及如何做?

最佳答案

我认为如果没有

,您将无法在同一解决方案下从另一个项目访问文件夹
  1. 引用dll
  2. 共享文件夹
  3. 维护资源文件
  4. 固定路径
  5. 如果你想访问文件然后创建链接文件

我认为共享文件夹是更好的解决方案:

http://support.microsoft.com/kb/324267

在您的网络配置中使用固定路径,并在需要时阅读它。我写了一些代码来获取同一项目中的文件夹名称。

API 方法:

[HttpGet]
public IList<string> GetFolderNames(int id)
{
    //FIND ALL FOLDERS IN FOLDER with in own project
    string location = System.Web.HttpContext.Current.Server.MapPath("parent folder name");

    //For fixed path location will be like as string location =@"E:\Target Folder\";

    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(location);
    var folderList = new List<string>();

    foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
    {
        //LOAD FOLDERS 
        folderList.Add(g.FullName);
    }

    return folderList;
}

JS 方法:

 //service name
 var serviceName = 'api controller name';
 //full url
 var remoteService = "Full Url" + serviceName;
 //parameters
 var params = {
     id: 1,
 };
 //get method calling
 $http({
     url: remoteService + '/GetFolderNames',
     method: 'GET',
     params: params,
 }).then(function (result) {
     //required result
     var r = result;
 }).
 catch (function (e) {
     throw e;
 });

服务器映射路径的使用:

Server.MapPath(".") returns D:\WebApps\shop\products
Server.MapPath("..") returns D:\WebApps\shop
Server.MapPath("~") returns D:\WebApps\shop
Server.MapPath("/") returns C:\Inetpub\wwwroot
Server.MapPath("/shop") returns D:\WebApps\shop

关于c# - 如何从 Asp.net 中的 web api 访问客户端项目目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478886/

相关文章:

c# - 指数数组的边界之外 。但是所有数组都是正确的

c# - CRM 2011,插件中的日期和夏令时

c# - 调用扩展方法的设置结果

c# - 为什么要在 Component 接口(interface)中声明复合方法?

asp.net - 如何动态添加元标签到页面?

c# - 从类文件访问母版页属性

javascript - 使用 moment.js (时区)时以 ("YYYY-MM-DD HH:mm") 格式返回日期和时间

c# - 我想在 asp.net c# 中生成字母数字 otp

javascript - 亚马逊 DynamoDB 和 AngularJS

javascript - 从具有隔离范围的指令内的指令访问 Controller