Can I have a webapi method
我可以使用webapi方法吗?
[Route("someroute")]
public void SomeMethod(string variable1, int variable2, Guid variable3)
{
//... Code here
}
simple json
var jsonvariable = new {
variable1:"somestring",
variable2:3,
variable3: {9E57402D-8EF8-45DE-B981-B8EC201D3D8E}
}
Then make the post
然后发帖子
HttpClient client = new HttpClient { BaseAddress = new Uri(ConfigurationManager.AppSettings["SomeURL"]) };
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.PostAsJsonAsync("someroute", jsonvariable ).Result
Coming from javascript I can do something like this and it resolves the individual properties but I can't seem to do it with a C# call
来自javascript我可以做这样的事情,它解决了各个属性,但我似乎无法用C#调用
var postData = {
appUniqueId: appUniqueId
};
$http
.post('someurl', postData)
.success(function(response) {
defer.resolve(response.data);
});
webapi method
SomeWebMethod(Guid appUniqueId) <-- same name as in postdata
1 个解决方案
#1
1
You can't quite go about it like you are doing there.
你不能像在那里那样去做。
As mentioned in the comments and in this article, you may only use one FromBody parameter for your method. Thus, one or none of those parameters are going to be bound from your JSON body. If any of them, it may be the guid that it tries to bind simply because of the web api parameter binding rules involved.
如评论和本文中所述,您只能为方法使用一个FromBody参数。因此,这些参数中的一个或者都不会从您的JSON主体绑定。如果它们中的任何一个,它可能是它试图绑定的guid,因为涉及的web api参数绑定规则。
How do you get around this? Make a class that is how your data is described.
你怎么解决这个问题?创建一个类,描述您的数据。
public class SomeBodyParameters
{
public string variable1 { get; set; }
public int variable2 { get; set; }
public Guid variable3 { get; set; }
}
Then your api method would look like this:
然后你的api方法看起来像这样:
[Route("someroute")]
public void SomeMethod([FromBody]SomeBodyParameters parameters)
{
//... Code here
}
The stipulation is that it won't automagically bind based on type, but rather the name that you set in your example anonymous type. variable1, variable2, and variable3 must have their names match in both spots.
规定是它不会基于类型自动绑定,而是您在示例匿名类型中设置的名称。 variable1,variable2和variable3的名称必须在两个位置都匹配。
When you think about it, that anonymous type that looks like JSON really is passing in a sort of object, so really that's what you should be accepting back anyway.
当你想到它时,那个看起来像JSON的匿名类型确实是在传递一种对象,所以真的那就是你应该接受的东西。
Food for thought: How would you be able to differentiate between a simple "string" and an object that has a singular string like var jsonvariable = new { variable1:"somestring"};
? You basically couldn't and the two probably shouldn't be equivilant.
深思熟虑:你如何能够区分一个简单的“字符串”和一个具有奇异字符串的对象,如var jsonvariable = new {variable1:“somestring”} ;?你基本上不可能,这两个可能不应该是等同的。
#1
1
You can't quite go about it like you are doing there.
你不能像在那里那样去做。
As mentioned in the comments and in this article, you may only use one FromBody parameter for your method. Thus, one or none of those parameters are going to be bound from your JSON body. If any of them, it may be the guid that it tries to bind simply because of the web api parameter binding rules involved.
如评论和本文中所述,您只能为方法使用一个FromBody参数。因此,这些参数中的一个或者都不会从您的JSON主体绑定。如果它们中的任何一个,它可能是它试图绑定的guid,因为涉及的web api参数绑定规则。
How do you get around this? Make a class that is how your data is described.
你怎么解决这个问题?创建一个类,描述您的数据。
public class SomeBodyParameters
{
public string variable1 { get; set; }
public int variable2 { get; set; }
public Guid variable3 { get; set; }
}
Then your api method would look like this:
然后你的api方法看起来像这样:
[Route("someroute")]
public void SomeMethod([FromBody]SomeBodyParameters parameters)
{
//... Code here
}
The stipulation is that it won't automagically bind based on type, but rather the name that you set in your example anonymous type. variable1, variable2, and variable3 must have their names match in both spots.
规定是它不会基于类型自动绑定,而是您在示例匿名类型中设置的名称。 variable1,variable2和variable3的名称必须在两个位置都匹配。
When you think about it, that anonymous type that looks like JSON really is passing in a sort of object, so really that's what you should be accepting back anyway.
当你想到它时,那个看起来像JSON的匿名类型确实是在传递一种对象,所以真的那就是你应该接受的东西。
Food for thought: How would you be able to differentiate between a simple "string" and an object that has a singular string like var jsonvariable = new { variable1:"somestring"};
? You basically couldn't and the two probably shouldn't be equivilant.
深思熟虑:你如何能够区分一个简单的“字符串”和一个具有奇异字符串的对象,如var jsonvariable = new {variable1:“somestring”} ;?你基本上不可能,这两个可能不应该是等同的。