【问题标题】:Reading FromUri and FromBody at the same time同时读取 FromUri 和 FromBody
【发布时间】:2012-08-17 19:50:21
【问题描述】:

我在 web api 中有一个新方法

[HttpPost]
public ApiResponse PushMessage( [FromUri] string x, [FromUri] string y, [FromBody] Request Request)

请求类在哪里

public class Request
{
    public string Message { get; set; }
    public bool TestingMode { get; set; }
}

我正在使用 PostBody 查询 localhost/Pusher/PushMessage?x=foo&y=bar:

{ Message: "foobar" , TestingMode:true }

我错过了什么吗?

【问题讨论】:

    标签: c# asp.net asp.net-web-api http-post frombodyattribute


    【解决方案1】:

    帖子正文通常是这样的 URI 字符串:

    Message=foobar&TestingMode=true
    

    您必须确保 HTTP 标头包含

    Content-Type: application/x-www-form-urlencoded
    

    编辑:因为它仍然无法正常工作,所以我自己创建了一个完整的示例。
    它打印正确的数据。
    我还使用了 .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));
        }
    }
    

    【讨论】:

    • 只有当我使用mvc结构时才成立。然而,这是 web api,所以绑定与 mvc 不同。不过谢谢你的回复!
    • 确保 HTTP 标头包含 Content-Type: application/x-www-form-urlencoded。
    • 你不能像纯文本一样发布到 web api mvc :S
    • 在您的 HTTP 标头 Web API 中使用它应该知道您有一个 url 编码的正文。你能分享你的HTTP Header吗?
    • WebClient 客户端 = new WebClient(); client.UploadStringCompleted += client_UploadStringCompleted; client.Headers["Content-Type"] = "application/x-www-form-urlencoded"; client.UploadStringAsync(new Uri(URL), "POST", "ApplicationKey=asdasda&PusherKey=asdasdasd");为了测试您的建议,我创建了一个新方法,例如 public ApiResponse PushMessage(string ApplicationKey, string PusherKey) 但它也不起作用,因为绑定在 RC 中发生了变化。它给出了内部服务器错误。
    【解决方案2】:

    Web API 使用命名规则。帖子的方法应该以 Post 开头。

    您应该将 PushMessage 重命名为方法名称 PostMessage。

    此外,Web api 默认侦听(取决于您的路由)“api/values/Message”而不是 Pusher/Pushmessage。

    [HttpPost] 属性不是必需的

    【讨论】:

    • post的方法应该以Post开头。 : 不需要
    • 这个答案是错误的。只有在没有 [HttpPost] 属性时,该方法才需要名称中的“Post”。
    【解决方案3】:

    您可以使用以下代码在请求正文中发布 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;
    

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 1970-01-01
      • 2015-11-11
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2019-06-09
      相关资源
      最近更新 更多