Angularjs 如何将结构化数据作为HTTP GET请求的参数包含?

Angularjs 如何将结构化数据作为HTTP GET请求的参数包含?,angularjs,rest,asp.net-web-api2,Angularjs,Rest,Asp.net Web Api2,我想将结构化数据作为参数包含到我的HTTP GET调用中,并认为我已经正确设置了它,但是我的API没有像我预期的那样接收数据。如何设置双方来交流结构化数据 我的angular应用程序向用WebAPI 2编写的RESTAPI提出了一个复杂的问题 控制器方法定义为: [RoutePrefix("v1/questions")] public class VersionsController : ApiController { [Route("askComplicated")] [Htt

我想将结构化数据作为参数包含到我的HTTP GET调用中,并认为我已经正确设置了它,但是我的API没有像我预期的那样接收数据。如何设置双方来交流结构化数据

我的angular应用程序向用WebAPI 2编写的RESTAPI提出了一个复杂的问题

控制器方法定义为:

[RoutePrefix("v1/questions")]
public class VersionsController : ApiController
{
    [Route("askComplicated")]
    [HttpGet]
    [ResponseType(typeof(ComplicatedResponseDto))]
    public IHttpActionResult AskComplicatedQuestion(ComplicatedRequestDto complicatedRequest)
    {
        var responseElements = new List<ComplicatedResponseElementDto>();
        foreach (var requestElement in complicatedRequest.Elements)
        {
            responseElements.Add(new ComplicatedResponseElementDto()
            {
                Id = requestElement.Id,
                answer = "it is unknowable!!!"
            });
        }
        return Ok(new ComplicatedResponseDto() { Elements = responseElements.ToArray()});
    }
我这样分解是因为我喜欢一个简单的请求和一个简单的响应。我正在采用对我过去的WCF工作最有效的模式(并且理解这可能是我的问题)

从角度来看,我的代码如下:

    var request = $http({
        method: "get",
        url: "http://localhost:65520/v1/questions/askComplicated",
        data: {
            complicatedRequest : {
                elements: [
                  { id: 2, question: 'what is the meaning of life?' },
                  { id: 3, question: 'why am I here?' },
                  { id: 4, question: 'what stock should I pick?' }
                ]
            }
        }
    });
    return request.then(handleSuccess, handleError);

当我从angular应用程序调用RESTAPI时,WebAPI2应用程序接收到它,但complementdRequestdTo为null。如何正确发送该数据以使其通过?

您应该在方法参数之前添加[FromUri]一个操作,以通知WebAPI您希望从应用程序Uri加载此参数,这就是使用GET-http action with data参数时数据的去向

所以你的代码应该是这样的

[RoutePrefix("v1/questions")]
public class VersionsController : ApiController
{
    [Route("askComplicated")]
    [HttpGet]
    [ResponseType(typeof(ComplicatedResponseDto))]
    public IHttpActionResult AskComplicatedQuestion([FromUri]ComplicatedRequestDto complicatedRequest)
    {
        var responseElements = new List<ComplicatedResponseElementDto>();
        foreach (var requestElement in complicatedRequest.Elements)
        {
            responseElements.Add(new ComplicatedResponseElementDto()
            {
                Id = requestElement.Id,
                answer = "it is unknowable!!!"
            });
        }
        return Ok(new ComplicatedResponseDto() { Elements = responseElements.ToArray()});
    }
最后,提供JavaScript代码示例如下(我只有jQuery,但对于Angular,负载将是相同的):


从Get to Post更新您的API,因为当我们想要传递复杂对象时,它包含更多数据,并且我们有限制在URL中发送最大字符。 执行以下修改:

[RoutePrefix("v1/questions")]
public class VersionsController : ApiController
{
    [Route("askComplicated")]
    [HttpPost]
    [ResponseType(typeof(ComplicatedResponseDto))]
    public IHttpActionResult AskComplicatedQuestion([FromBody]ComplicatedRequestDto complicatedRequest)
    {
        //write ur code here...
    }
}
更新您的js代码,如下所示:

 var request = $http({
        method: "post",
        url: "http://localhost:65520/v1/questions/askComplicated",  
        data: {
            complicatedRequest : {
                elements: [
                  { id: 2, question: 'what is the meaning of life?' },
                  { id: 3, question: 'why am I here?' },
                  { id: 4, question: 'what stock should I pick?' }
                ]
            },
        }
    });
    return request.then(handleSuccess, handleError);

进行更改后,请求仍然为空。我会调查mofre关于FromUri的情况,看看我是否做错了什么。嗨,Michael,请检查我的最新答案。现在应该可以用了。在代码的JS部分,您有额外的字段,因此ASP无法反序列化您的对象。所以,您需要添加FromUri anotation并修复JS。当我这样做时,它会返回405 method not allowed。现在我再次查看它,这对我来说很有用。谢谢
$.ajax({url:"/v1/questions/askComplicated",data: {
  "Elements": [
    {
      "Id": 2,
      "Question": "what is the meaning of life?"
    },
    {
      "Id": 3,
      "Question": "why am I here?"
    }
  ]
}})
[RoutePrefix("v1/questions")]
public class VersionsController : ApiController
{
    [Route("askComplicated")]
    [HttpPost]
    [ResponseType(typeof(ComplicatedResponseDto))]
    public IHttpActionResult AskComplicatedQuestion([FromBody]ComplicatedRequestDto complicatedRequest)
    {
        //write ur code here...
    }
}
 var request = $http({
        method: "post",
        url: "http://localhost:65520/v1/questions/askComplicated",  
        data: {
            complicatedRequest : {
                elements: [
                  { id: 2, question: 'what is the meaning of life?' },
                  { id: 3, question: 'why am I here?' },
                  { id: 4, question: 'what stock should I pick?' }
                ]
            },
        }
    });
    return request.then(handleSuccess, handleError);