相关疑难解决方法(0)

发布到Web API时出现不支持的媒体类型错误

制作一个Windows手机应用程序,虽然我可能很容易从我的网络Api拉出来,但我很难发布到它.每当发布到api时,我都会收到"不支持的媒体类型"错误消息,并且我不确定为什么会发生这种情况,因为我使用的类作为我的JSON帖子的基础与api中使用的类相同.

PostQuote(后方法)

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//
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

行情和主题(模型) …

.net c# json windows-phone asp.net-web-api

28
推荐指数
2
解决办法
4万
查看次数

标签 统计

.net ×1

asp.net-web-api ×1

c# ×1

json ×1

windows-phone ×1