admin管理员组文章数量:1431927
I am developing angular js with web api.
I have this cotroller: Controller/MasterController, and thiis in my WebApi Config:
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I call this function from Global.asax, Application_Start event.
I call my web api from service.js like that:
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;
};
In debug mode, I saw that I reach this area. I get this error in chrome console: "Failed to load resource: the server responded with a status of 404"
and this is what he was trying to get to: http://localhost:12345/api/Master
Also, I tried to get to the web api controller directly through the browser, and I couldn't find it.
Thank You
I am developing angular js with web api.
I have this cotroller: Controller/MasterController, and thiis in my WebApi Config:
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I call this function from Global.asax, Application_Start event.
I call my web api from service.js like that:
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;
};
In debug mode, I saw that I reach this area. I get this error in chrome console: "Failed to load resource: the server responded with a status of 404"
and this is what he was trying to get to: http://localhost:12345/api/Master
Also, I tried to get to the web api controller directly through the browser, and I couldn't find it.
Thank You
Share Improve this question edited Apr 23, 2014 at 13:09 aludvigsen 6,0013 gold badges29 silver badges37 bronze badges asked Apr 23, 2014 at 13:07 HS1HS1 6482 gold badges12 silver badges28 bronze badges 4- What does your MasterController look like? The following should work if you use localhost:12345/api/Master using System.Web.Http; namespace WebApi.Controllers { public class MasterController : ApiController { public string Get() { return "Hello world"; } } } – h3li0s Commented Apr 23, 2014 at 13:15
- public class MasterController : ApiController { // GET api/<controller> public IEnumerable<MenuItem> Get() { //SOME FUNCTION } } – HS1 Commented Apr 23, 2014 at 13:18
- I tried what you proposed, didn't work – HS1 Commented Apr 23, 2014 at 13:24
-
I would bet this is a server-side configuration problem. What I would do is create a simple page with jQuery, then call the server using
$.ajax()
from Firebug; tweak the configuration and repeat until your call succeeds. Also: have you tried aGet(int id)
method signature? (orint?
since it is optional - I do not know C# well enough) – Nikos Paraskevopoulos Commented Apr 23, 2014 at 13:34
2 Answers
Reset to default 0Just to be sure your API is working try the following:
Global.asax.cs:
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
MasterController:
using System.Web.Http;
namespace WebApi.Controllers
{
public class MasterController : ApiController
{
public string Get()
{
return "Hello world";
}
}
}
Calling http:// localhost:[SomePort]/api/Master in your browser should result in this: "Hello world"
The configurations above are all standard when creating a new WebApi.
After trying some new directions, I received new error details. The final error I received + its solution you can find here
本文标签: javascriptWeb APIFailed to load resource the server responded with a status of 404Stack Overflow
版权声明:本文标题:javascript - Web API - Failed to load resource: the server responded with a status of 404 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745583648a2664767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论