我是AngularJS和WebAPI的新手。我有兩個Visual Studio 2015項目,EventViewer(AngularJS)和AppData(WebAPI),並配置了IIS Express 7來託管網站。我有AngularJS項目的路徑作爲網站的路徑,而且工作正常。對於AppData項目(WebAPI),我向該網站添加了一個應用程序。作爲網站應用程序的路徑,我正在使用包含WebAPI項目的文件夾的物理路徑。這是正確的嗎?是否足以使angular和webAPI應用程序相互交談? AngularJS應用似乎沒有與WebAPI應用進行通信。當我從IIS瀏覽網站時,我得到正確的頁面但沒有數據。我錯過了什麼?任何幫助將不勝感激。AngularJS項目不與WebAPI項目溝通
AngularJS控制器:
'use strict';
eventsApp.controller('EventListController',
function EventListController($scope, eventData) {
$scope.events = eventData.getAllEvents();
});
AngularJS服務:
eventsApp.factory('eventData', function ($resource) {
var resource = $resource('/data/event/:id', { id: '@id' }, { "getAll": { method: "GET", isArray: true, params: { something: "foo" } } });
return {
getAllEvents: function() {
return resource.query();
}
};
});
的WebAPI控制器:
namespace AppData.Controllers
{
public class EventController : ApiController
{
public JToken Get(string id = null)
{
if (id == null)
return GetAllJsonEventsAsArray();
return GetSingleJsonFile(id);
}
private static JToken GetSingleJsonFile(string id)
{
var path = System.Web.Hosting.HostingEnvironment.MapPath("/");
return JObject.Parse(System.IO.File.ReadAllText(path + "../app/data/event/" + id + ".json"));
}
public void Post(string id, JObject eventData)
{
var path = System.Web.Hosting.HostingEnvironment.MapPath("/");
System.IO.File.WriteAllText(path + "../app/data/event/" + id + ".json", eventData.ToString(Formatting.None));
}
private JArray GetAllJsonEventsAsArray()
{
var path = System.Web.Hosting.HostingEnvironment.MapPath("/");
var contents = "";
foreach (var file in System.IO.Directory.GetFiles(path + "../app/data/event/"))
{
contents += System.IO.File.ReadAllText(file) + ",";
}
return JArray.Parse("[" + contents.Substring(0, contents.Length - 1) + "]");
}
}
}