【问题标题】:How to call multiple get methods in Web API如何在 Web API 中调用多个 get 方法
【发布时间】:2017-02-24 22:37:01
【问题描述】:

您好,我正在使用 angularjs 开发 web api 应用程序。我在一个控制器中有 GET 三种方法,但它们都没有调用。例如,

 public class usersController : ApiController
    {
        //First Get method.
        [HttpGet]
        [SuperAdmin]
        [LoginCheck]
        [ActionName("me")]
        public HttpResponseMessage me()
        {
        }

        //second Get method.
        [LoginCheck]
        [SuperAdmin]
        public IHttpActionResult Get(int id)
        {

        }

        //third Get method.
        [HttpGet]
        [LoginCheck]
        [SuperAdmin]
        public HttpResponseMessage Get(string role)
        {
        }

我正在使用以下代码调用。

this.getSubs = function () {
        var role = "SUPER_ADMIN";
        //role is optional here
        var url = '/api/users/' + role;
        return $http.get(url).then(function (response) {
            return response.data;
        });
    }
    this.getcurrentuser = function () {
        var url = '/api/users/me/';
        return $http.get(url).then(function (response) {
            return response.data;
        });
    }
    this.getSubsbyID = function (id) {
        var url = '/api/users/' + id;
        return $http.get(url).then(function (response) {
            return response.data;
        });
    }

我的路由如下。

 config.Routes.MapHttpRoute(
                name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

我无法使用 angularjs 调用三种方法中的任何一种。我可以知道我在哪里做错路由吗?任何帮助,将不胜感激。谢谢你

【问题讨论】:

  • 您能否发布实现这些方法的服务(或控制器)?此问题可能有多种原因,请提供更多信息。
  • 如果您认为是路由问题,请同时发布您的路由
  • 谢谢。我加载资源失败:服务器响应状态为 400(错误请求)localhost:22045/api/users/SUPER_ADMIN
  • 您是否使用任何代理服务器,例如 apache 或 nginx?
  • 不,我在 localhost 中运行。

标签: angularjs asp.net-web-api routing


【解决方案1】:

两件事一是这可能是您的请求是 CORS 请求的问题。您可能需要启用 CORS。首先,您需要安装 nuget 包 Microsoft.AspNet.WebApi.Cors,然后在路由调用之前添加到您的 api 配置文件中。

config.EnableCors();

其次,调整路由以使用属性路由可能会有所帮助。 WebApi 不像普通的 MVC 控制器那样支持动作路由。您可能已经在配置文件中设置了一些路由,例如

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

这将允许您调用控制器用户,但我发现尝试让 ApiController 使用签名调用适当的 GET 方法是有问题的。我建议您使用属性路由。如果还没有,您可以将属性路由添加到您的配置中

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

然后你可以像这样设置你的 HttpGet 属性路由。

public class usersController : ApiController
{
        //First Get method.
        [HttpGet]
        [SuperAdmin]
        [LoginCheck]
        [Route("api/users/me")] // this is your routing attribute
        public HttpResponseMessage me()
        {
        }

        //second Get method.
        [LoginCheck]
        [SuperAdmin]
        [Route("api/users/{id:int}")]
        public IHttpActionResult Get(int id)
        {

        }

        //third Get method.
        [HttpGet]
        [LoginCheck]
        [SuperAdmin]
        [Route("api/users/{role}")]
        public HttpResponseMessage Get(string role)
        {
}

希望这会有所帮助。

【讨论】:

  • 谢谢。我在页面加载时调用了三个 api。在页面加载 3 次时,我点击了相同的网址。 [Route("api/users/{role}")] public HttpResponseMessage Get(字符串角色)
  • 您是否更新了路由配置并添加了 config.MapHttpAttributeRoutes();除非该行存在,否则 [Route] 将不起作用。这也需要在任何其他路由之前,但在 config.EnableCors(); 之后(如果您使用的是 cors)
  • Cors 我启用了,没问题。 config.MapHttpAttributeRoutes();我有。
  • 对不起,我忘了包含 int 约束通知 [Route("api/users/{id:int}")]
猜你喜欢
  • 2014-10-14
  • 1970-01-01
  • 2017-09-25
  • 2015-05-18
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多