第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取 Delete HttpResponseMessage 的屬性

獲取 Delete HttpResponseMessage 的屬性

C#
慕妹3242003 2021-08-29 17:50:34
我希望測試下面我的 API 請求之一的輸出。async Task DeleteNonExistantFoo(){    using (HttpClient client = new HttpClient())    {        client.BaseAddress = new Uri("Http://localhost:43240/");        client.DefaultRequestHeaders.Accept.Clear();        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));        using (HttpResponseMessage response = await client.DeleteAsync("api/foos/1"))        {            var responseContent = await response.Content.ReadAsStringAsync();            //Assert.AreEqual(?????, "A Foo with ID of 1 does not exist.");        }    }}我想從下面的響應(yīng)中獲取 exceptionMessage,當(dāng)我向 API 發(fā)送 DELETE 請求時,它作為響應(yīng)給出。在 Visual Studio 中調(diào)試時,它不會讓我查看響應(yīng)內(nèi)容對象,從而出現(xiàn)錯誤“responseContent 無法獲取局部變量或參數(shù)的值,因為它在此指令指針處不可用,可能是因為它已被優(yōu)化掉?!蔽沂欠裥枰獙⑵滢D(zhuǎn)換為 JSON 對象才能讀取它?{    "message": "An error has occurred.",    "exceptionMessage": "A Foo with ID of 1 does not exist.",    "exceptionType": "System.Exception",    "stackTrace": "} 
查看完整描述

1 回答

?
幕布斯6054654

TA貢獻1876條經(jīng)驗 獲得超7個贊

你可以嘗試使用Newtonsoft.Json的JsonConvert.DeserializeObject方法來讀取響應(yīng)的Json轉(zhuǎn)換成一個對象,并使用它。


創(chuàng)建一個類 ApiResponeMoedl


public class ApiResponeMoedl

{

    public string message { get; set; }

    public string exceptionMessage { get; set; }

    public string exceptionType { get; set; }

    public string stackTrace { get; set; }

}

然后使用JsonConvert.DeserializeObject<ApiResponeMoedl>將您的 json 數(shù)據(jù)反序列化為一個ApiResponeMoedl對象,然后使用對象的exceptionMessage屬性,您將獲得沙漠信息。


async Task DeleteNonExistantRedirect()

{

    using (HttpClient client = new HttpClient())

    {

        client.BaseAddress = new Uri("Http://localhost:43240/");

        client.DefaultRequestHeaders.Accept.Clear();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


        using (HttpResponseMessage response = await client.DeleteAsync("api/foos/1"))

        {

            var responseContent = await response.Content.ReadAsStringAsync();

            var respOjb = JsonConvert.DeserializeObject<ApiResponeMoedl>(responseContent);

            //respOjb.exceptionMessage

        }

    }

}


查看完整回答
反對 回復(fù) 2021-08-29
  • 1 回答
  • 0 關(guān)注
  • 203 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號