张贴到网页API时不支持的媒体类型错误 [英] Unsupported Media Type error when posting to Web API

查看:484
本文介绍了张贴到网页API时不支持的媒体类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作一个Windows Phone应用程序,虽然我可能很容易从我的Web API提取我无法张贴到它。无论何时发布到API,我得到了不支持的媒体类型错误消息,我不知道,为什么它发生在考虑我使用的基地我JSON职位类别是一样的,在API中使用的。

PostQuote(POST方法)

 专用异步无效PostQuote(对象发件人,RoutedEventArgs E)
        {
            行情postquote =新行情(){
                QuoteId = CURRENTCOUNT,
                QuoteText = Quote_Text.Text,
                QuoteAuthor = Quote_Author.Text,
                TopicId = 1019
            };
            JSON字符串= JsonConvert.SerializeObject(postquote);
            如果(Quote_Text.Text =!&放大器;&放大器;!Quote_Author.Text =){                使用(HttpClient的HC =新的HttpClient())
                {
                    hc.BaseAddress =新的URI(http://rippahquotes.azurewebsites.net/api/QuotesApi);
                    hc.DefaultRequestHeaders.Accept.Clear();
                    hc.DefaultRequestHeaders.Accept.Add(新System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(应用/ JSON));
                    HTT presponseMessage响应=等待hc.PostAsync(hc.BaseAddress,新的StringContent(JSON));
                    如果(response.IsSuccessStatus code)
                    {
                        Frame.Navigate(typeof运算(的MainPage));
                    }
                    其他
                    {
                        Quote_Text.Text = response.Status code.ToString();
                        //返回不支持的媒体类型//
                    }
                }
            }
        }

行情和主题(模型)

 公共类行情
    {
        公众诠释QuoteId {搞定;组; }
        公众诠释TopicId {搞定;组; }
        公共字符串QuoteText {搞定;组; }
        公共字符串QuoteAuthor {搞定;组; }
        公共主题主题{搞定;组; }
        公共字符串QuoteEffect {搞定;组; }
    }
    //主题模型的//
    公共类主题
    {
        公众诠释TopicId {搞定;组; }
        公共字符串TopicName {搞定;组; }
        公共字符串TopicDescription {搞定;组; }
        公众诠释TopicAmount {搞定;组; }
    }


解决方案

正如你在<一见href=\"http://blogs.msdn.com/b/wsdevsol/archive/2013/02/05/how-to-use-httpclient-to-post-json-data.aspx\">this和<一个href=\"https://monkeyweekend.word$p$pss.com/2014/10/23/how-to-send-text-json-or-files-using-httpclient-postasync/\">this文章,你应该在创建的StringContent时所设置的媒体类型

 新的StringContent(JSON,Encoding.UTF32,应用/ JSON);

Making a windows phone application and although I may easily pull from my Web Api I am having trouble posting to it. Whenever posting to the api I get the "Unsupported Media Type" error message and I'm not sure as to why it is happening considering the class I using as the base for my JSON post is the same as the one used in the api.

PostQuote (Post Method)

private async void PostQuote(object sender, RoutedEventArgs e)
        {
            Quotes postquote = new Quotes(){
                QuoteId = currentcount,
                QuoteText = Quote_Text.Text,
                QuoteAuthor = Quote_Author.Text,
                TopicId = 1019
            };
            string json = JsonConvert.SerializeObject(postquote);
            if (Quote_Text.Text != "" && Quote_Author.Text != ""){

                using (HttpClient hc = new HttpClient())
                {
                    hc.BaseAddress = new Uri("http://rippahquotes.azurewebsites.net/api/QuotesApi");
                    hc.DefaultRequestHeaders.Accept.Clear();
                    hc.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await hc.PostAsync(hc.BaseAddress, new StringContent(json));
                    if (response.IsSuccessStatusCode)
                    {
                        Frame.Navigate(typeof(MainPage));
                    }
                    else
                    {
                        Quote_Text.Text = response.StatusCode.ToString();
                        //Returning Unsupported Media Type//
                    }
                }
            }
        }

Quotes and Topic (Model)

public class Quotes
    {
        public int QuoteId { get; set; }
        public int TopicId { get; set; }
        public string QuoteText { get; set; }
        public string QuoteAuthor { get; set; }
        public Topic Topic { get; set; }
        public string QuoteEffect { get; set; }
    }
    //Topic Model//
    public class Topic
    {
        public int TopicId { get; set; }
        public string TopicName { get; set; }
        public string TopicDescription { get; set; }
        public int TopicAmount { get; set; }
    }

解决方案

As you can see in this and this articles, you should set the media type when creating StringContent

new StringContent(json, Encoding.UTF32, "application/json");

这篇关于张贴到网页API时不支持的媒体类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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