当我从http://localhost:5086/发出GET请求时效果很好,发送到 api / FileBrowser ,但当我尝试从http://localhost:5086/Home/Index向api发出请求时,添加到网址controller主页和请求发送到 / Home / api / FileBrowser
请帮助我做错了什么?
带有2种方法的ApiController
public FileBrowserModel Get()
{
var result = new FileBrowserModel();
List<string> drives = new List<string>();
foreach (var drive in DriveInfo.GetDrives())
{
var path = drive.Name;
FileManagerCounter fmCounter = new FileManagerCounter(path);
result.CountTo10Mb += fmCounter.CountTo10Mb;
result.Countfrom10To50Mb += fmCounter.Countfrom10To50Mb;
result.CountFrom100Mb += fmCounter.CountFrom100Mb;
drives.Add(path);
}
result.SubDirectories = new List<string>(drives);
return result;
}
[CacheOutput(ServerTimeSpan = 150)]
[System.Web.Http.HttpGet]
public FileBrowserModel Get(string path)
{
var result = new FileBrowserModel();
try
{
try
{
result.ParentPath = Directory.GetParent(path).FullName;
}
catch (Exception)
{
}
var files = Directory.GetFiles(path).ToList();
result.Files = new List<string>(files);
result.CurrentPath = path;
FileManagerCounter fmCounter = new FileManagerCounter(path);
result.CountTo10Mb = fmCounter.CountTo10Mb;
result.Countfrom10To50Mb = fmCounter.Countfrom10To50Mb;
result.CountFrom100Mb = fmCounter.CountFrom100Mb;
var subDirectories = Directory.GetDirectories(path).ToList();
result.SubDirectories = new List<string>(subDirectories);
}
catch (Exception ex)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
return result;
}
js文件
angular.module("FileBrowserApp", [])
.controller("fileBrowserController", function($scope, $http) {
$scope.getFileBrowsing = function (path) {
$scope.isLoading = true;
if (path != null) {
$http({
url: "api/FileBrowser?path=" + encodeURIComponent(path),
method: "GET"
}).success(function (data) {
$scope.fileBrowserModel = data;
$scope.isLoading = false;
});
} else {
$http({
url: "api/FileBrowser",
method: "GET"
}).success(function (data) {
$scope.fileBrowserModel = data;
$scope.isLoading = false;
});
}
};
$scope.getFileBrowsing();
});
路线
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{path}",
defaults: new { path = RouteParameter.Optional }
);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
答案 0 :(得分:2)
编辑:War10ck的答案是简单的解决方案。
以下是正确构建相对网址的替代解决方案
问题是,您将url值硬编码为“api / FileBrowser”。因此,基于当前页面,此值将会更改。如果您在主页上,它将是Home/api/FileBrowser
。
你应该做的是建立正确的相对网址。由于您使用的是asp.net mvc页面,因此您可以使用Url.Action帮助程序方法来构建正确的相对URL。您可以在剃刀视图中生成应用程序路径的值,并将其传递给角度控制器/数据服务并使用它。
所以在你的剃刀视图中,
<script>
var myApp = myApp || {};
myApp.Urls = myApp.Urls || {};
myApp.Urls.baseUrl = '@Url.Content("~")';
</script>
<script src="~/Scripts/AngularControllerForPage.js"></script>
<script>
var a = angular.module("FileBrowserApp").value("appSettings", myApp);
</script>
在角度控制器中,您可以访问此appSettings。
var app = angular.module("FileBrowserApp", []);
var ctrl = function (appSettings) {
var vm = this;
vm.baseUrl = appSettings.Urls.baseUrl;
//build other urls using the base url now
var fileBrowserUrl= vm.baseUrl + "api/FileBrowser";
alert(fileBrowserUrl);
//You can use this variable value for your http call now
$http({
url: fileBrowserUrl+ "?path=" + encodeURIComponent(path),
method: "GET"
}).success(function (data) {
$scope.fileBrowserModel = data;
$scope.isLoading = false;
});
};
app.controller("fileBrowserController", ctrl)
您可以 在您的数据服务和指令等中访问它,并使用它来构建正确的网址。
您也可以考虑将您的http调用从角度控制器移动到数据服务。
答案 1 :(得分:2)
如果您完全确定您的Web API图层将始终存在于根域之上的一层,那么您可以将ajax调用中的网址更改为:
url: "/api/..."
无论网站的结构如何,都会相对于根域调用api。