【发布时间】:2016-04-08 14:39:16
【问题描述】:
我在从我的角度代码调用 http get 到 WebApi 控制器时遇到问题。我使用 ASP MVC 只是为了提供起始页,起始页 url 看起来像:http://localhost:23845/StudentsEditor/StudentsView 现在从角度我调用 http 请求:
angular.element(document).ready(function () {
$http({
method: "GET",
url: "api/Groups/GetGroups",
dataType: "json",
}).then(function successCallback(response) {
$scope.groups = response.data;
}, function errorCallback(response) {
alert("trouble...");
});
我收到 404,因为 URL 不正确。它连接路径,它看起来像: 获取http://localhost:23845/StudentsEditor/api/Groups/GetGroups 而不是http://localhost:23845/api/Groups/GetGroups
请给我一些建议来解决它。当然我已经定义了RouteConfig: 公共静态无效注册路由(RouteCollection 路由) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "StudentsEditor", action = "StudentsView", id = UrlParameter.Optional }
);
和 webApi 配置:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{url}/api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
【问题讨论】:
标签: angularjs asp.net-mvc http http-status-code-404