C# 同时读取 FromUri 和 FromBody
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12072277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading FromUri and FromBody at the same time
提问by kkocabiyik
I have a new method in web api
我在 web api 中有一个新方法
[HttpPost]
public ApiResponse PushMessage( [FromUri] string x, [FromUri] string y, [FromBody] Request Request)
where request class is like
请求类就像
public class Request
{
public string Message { get; set; }
public bool TestingMode { get; set; }
}
I'm making a query to localhost/Pusher/PushMessage?x=foo&y=bar with PostBody:
我正在使用 PostBody 对 localhost/Pusher/PushMessage?x=foo&y=bar 进行查询:
{ Message: "foobar" , TestingMode:true }
Am i missing something?
我错过了什么吗?
采纳答案by Johannes Egger
A post body is typically a URI string like this:
帖子正文通常是一个 URI 字符串,如下所示:
Message=foobar&TestingMode=true
You have to make sure that the HTTP header contains
您必须确保 HTTP 标头包含
Content-Type: application/x-www-form-urlencoded
EDIT: Because it's still not working, I created a full example myself.
It prints the correct data.
I also used .NET 4.5 RC.
编辑:因为它仍然无法正常工作,所以我自己创建了一个完整的示例。
它打印正确的数据。
我还使用了 .NET 4.5 RC。
// server-side
public class ValuesController : ApiController {
[HttpPost]
public string PushMessage([FromUri] string x, [FromUri] string y, [FromBody] Person p) {
return p.ToString();
}
}
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public override string ToString() {
return this.Name + ": " + this.Age;
}
}
// client-side
public class Program {
private static readonly string URL = "http://localhost:6299/api/values/PushMessage?x=asd&y=qwe";
public static void Main(string[] args) {
NameValueCollection data = new NameValueCollection();
data.Add("Name", "Johannes");
data.Add("Age", "24");
WebClient client = new WebClient();
client.UploadValuesCompleted += UploadValuesCompleted;
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
Task t = client.UploadValuesTaskAsync(new Uri(URL), "POST", data);
t.Wait();
}
private static void UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e) {
Console.WriteLine(Encoding.ASCII.GetString(e.Result));
}
}
回答by napp
You may use following code to post json in request body:
您可以使用以下代码在请求正文中发布 json:
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Request request = new Request();
HttpResponseMessage response = httpClient.PostAsJsonAsync("http://localhost/Pusher/PushMessage?x=foo&y=bar", request).Result;
//check if (response.IsSuccessStatusCode)
var createResult = response.Content.ReadAsAsync<YourResultObject>().Result;
回答by Gertjan
The Web API uses naming regulations. The method for a post should be started with Post.
Web API 使用命名规则。帖子的方法应该以 Post 开头。
You should rename your PushMessage to method name PostMessage.
您应该将您的 PushMessage 重命名为方法名称 PostMessage。
Also the web api defaulty listens (depending on your route) to 'api/values/Message' and not to Pusher/Pushmessage.
此外,web api 默认侦听(取决于您的路由)“api/values/Message”而不是 Pusher/Pushmessage。
[HttpPost] attribute is not required
[HttpPost] 属性不是必需的

