我的 Asp.net 项目是多层项目,如 DAL
、DATA
、WebApi
.... 等。
我的UI层是html5
,angularjs
等组合
我的 UI 层中没有服务器端代码。
我的 UI 层通过 rest api 与 webapi
层通信。
现在我想要我的 UI 层的文件夹列表。我怎样才能做到这一点 ?因为客户端脚本无权执行此操作。所以,只剩下 web api 层。我是否可以从我的 web api 层获取 UI 层的目录列表?
如果是,我该怎么做?如果否,还有什么选择以及如何做?
最佳答案
我认为如果没有
,您将无法在同一解决方案下从另一个项目访问文件夹- 引用dll
- 共享文件夹
- 维护资源文件
- 固定路径
- 如果你想访问文件然后创建链接文件
我认为共享文件夹是更好的解决方案:
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/