[英]Web API - Failed to load resource: the server responded with a status of 404
I am developing angular js with web api. 我正在使用Web API开发Angular JS。
I have this cotroller: Controller/MasterController, and thiis in my WebApi Config: 我有这个控制者:Controller / MasterController,在我的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. 我从Global.asax的Application_Start事件中调用此函数。
I call my web api from service.js like that: 我像这样从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;
};
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"
我在chrome控制台中收到此错误: “无法加载资源:服务器响应状态为404”
and this is what he was trying to get to: http://localhost:12345/api/Master
这就是他想要的方法:
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. 另外,我尝试直接通过浏览器访问Web api控制器,但找不到。
Thank You 谢谢
Just to be sure your API is working try the following: 为确保您的API正常运行,请尝试以下操作:
Global.asax.cs: 的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: 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: 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" 在浏览器中调用http:// localhost:[SomePort] / api / Master会导致以下结果:“ Hello world”
The configurations above are all standard when creating a new WebApi. 创建新的WebApi时,以上配置均为标准配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.