目的:要在URL中傳遞一個(gè)參數(shù)到服務(wù)端。但是name參數(shù)中有保留字符/,所以先用UrlEncode進(jìn)行編碼。測(cè)試代碼如下:[Fact]public async void GetByName_TestAsync(){ var name = WebUtility.UrlEncode("aa/bbc"); var response = await Client.GetAsync($"api/users/{name}");
var user = await response.Content.ReadAsAsync<UserDto>();
user.Should().NotBeNull();
}服務(wù)端API:[HttpGet("{name}")]public async Task<UserDto> Get(string name){ return await _userAppService.GetUserByNameAsync(name);
}結(jié)果查不到name=aa/bbc的user。通過debug發(fā)現(xiàn),請(qǐng)求的結(jié)果是404,本來以為是MVC解碼了URL再進(jìn)行路由匹配,因此請(qǐng)求的路徑就變成了aa/bbc。但通過在瀏覽器中發(fā)起請(qǐng)求,就可以等到編碼后的值aa%2Fbbc,所以判斷這是HttpClient的問題,它在發(fā)起請(qǐng)求之前對(duì)URL作了手腳。那么如何通過URL傳遞帶有保留字符(/)的參數(shù)呢?
- 1 回答
- 0 關(guān)注
- 1250 瀏覽
添加回答
舉報(bào)
0/150
提交
取消