【发布时间】:2014-06-08 08:15:51
【问题描述】:
我正在使用 web api 开发 angular js。
我有这个控制器:Controller/MasterController,这在我的 WebApi 配置中:
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我从 Global.asax,Application_Start 事件中调用此函数。
我像这样从 service.js 调用我的 web api:
var service = function ($http) {
var _$http = $http;
self = this;
self.getMenuItems = function () {
var promise = _$http({
method: "GET",
url: 'api/Master'
}).success(function (data, status, headers, config) {
}).error(function (data, status, headers, config) {
});
return promise;
};
在调试模式下,我看到我到达了这个区域。 我在 chrome 控制台中收到此错误: “加载资源失败:服务器响应状态为 404”
这就是他想要达到的目标:http://localhost:12345/api/Master
另外,我尝试通过浏览器直接访问web api控制器,但找不到。
谢谢
【问题讨论】:
-
您的 MasterController 是什么样的?如果您使用 System.Web.Http 使用localhost:12345/api/Master,则以下内容应该有效;命名空间 WebApi.Controllers { public class MasterController : ApiController { public string Get() { return "Hello world"; } } }
-
public class MasterController : ApiController { // GET api/
public IEnumerable -
我试过你建议的,没用
-
我敢打赌这是一个服务器端配置问题。我要做的是用 jQuery 创建一个简单的页面,然后使用来自 Firebug 的
$.ajax()调用服务器;调整配置并重复,直到您的呼叫成功。另外:您是否尝试过Get(int id)方法签名? (或int?,因为它是可选的 - 我不太了解 C#)
标签: javascript angularjs asp.net-web-api