【问题标题】:How to Access client project directory from web api in Asp.net如何从 Asp.net 中的 web api 访问客户端项目目录
【发布时间】:2014-12-16 05:12:06
【问题描述】:

我的 Asp.net 项目是多层项目,例如 DAL,DATA,WebApi .... 等。

我的 UI 层是html5,angularjs 等的组合。

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

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

现在我想要我的 UI 层的文件夹列表。我怎样才能做到这一点 ?因为客户端脚本没有能力这样做。所以,只剩下 web api 层。我可以从我的 web api 层获取 UI 层的目录列表吗?

如果是,我该怎么做?如果否,有什么替代方法以及如何?

【问题讨论】:

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


    【解决方案1】:

    我认为如果没有

    ,您将无法从同一解决方案下的另一个项目访问文件夹
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2012-07-03
      相关资源
      最近更新 更多