Web Api 404错误找不到资源 [英] Web Api 404 error The resource cannot be found
问题描述
我尝试使用Web Api从服务器发送/获取数据.WebApiConfig.cs:
I try to use Web Api for send/get data from server. WebApiConfig.cs :
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}"
);
RouteConfig.cs:
RouteConfig.cs:
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 }
);
}
Globa.asax.cs:
Globa.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
Api控制器(ServicesController.cs):
Api controller (ServicesController.cs):
public class ServicesController : ApiController
{
public List<TreeMenuItem> LoadMetadata()
{
List<TreeMenuItem> itemsMenu = new List<TreeMenuItem>();
TreeMenuItem dataSource = new TreeMenuItem("1", "DataSources", null);
itemsMenu.Add(dataSource);
return itemsMenu;
}
}
我正在尝试从如下波纹管的angularJS控制器访问api:
I'm trying to access api from angularJS controller like bellow:
angular.module("App", ["ngRoute", "ngResource"])
.controller('MainController', ["$scope", "$http", MainController]);
function MainController($scope, $http) {
var baseUrl = "Services/LoadMetadata";
var params = {};
$http.post(baseUrl, params)
.then(function (data) {
$scope.roleList = data.data;
});
}
我在$ http.post上收到服务/LoadMetadata"错误404!我使用路由尝试了一些版本-相同的错误404.有帮助吗?
I get error 404 on $http.post for "Services/LoadMetadata"! I tried some version using route - same error 404. Any help?
推荐答案
更改注册顺序...
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
在route-config之前注册web-api-config,因为我猜想route-config正在干预web-api-config ...
register web-api-config before route-config because i guess route-config is meddling with the web-api-config...
还请考虑将所有api调用都加api的标准做法.
also please do consider the standard practice of having all your api calls prefixed with api.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
这篇关于Web Api 404错误找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!