Web API 2 DataMember命名在FromUri参数中不起作用 [英] Web API 2 DataMember naming not working in FromUri parameters

查看:139
本文介绍了Web API 2 DataMember命名在FromUri参数中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UriParameters中使用DataMember命名时遇到了问题.

I'm having issues with DataMember-naming in UriParameters.

[DataContract]
public class testobj 
{

[DataMember(Name = "Test")]
public string a {get; set; }

[DataMember(Name = "Test1")]
public string b {get; set; }

}

然后我有我的控制器:

public IHttpActionResult test([FromUri] testobj testparams)
{
   return testparams;
}

在响应中,我得到 Test Test1 ,这是正确的.但是我必须在uriParameters中使用 a b ,为什么我不能在那里使用Test和Test1?

In the response i get Test and Test1, that's correct. However i have to use a and b in the uriParameters, why can't i use Test and Test1 there?

我该如何解决?

推荐答案

您不能在查询字符串中传递对象类型,必须在HTTP数据包主体中传递对象类型,并在Controller方法的Parameter中使用[FromBody].

You cannot pass an object type in the Query String, You have to pass it in the HTTP Packets Body, and use [FromBody] in your Controller methods Parameter.

这是一个Stackoverflow链接,介绍了更多信息, ASP.NET Web APi-将对象作为参数传递

here's a Stackoverflow link explaining more, ASP.NET Web APi - Passing an object as parameter

谢谢,佛陀

这篇关于Web API 2 DataMember命名在FromUri参数中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆