我有一个方法,可以通过ID,名称和密码来调用托管在不同服务器中的Web API。 我的代码非常标准,如下所示:
[HttpGet("{id}")]
//[Route("api/GetMember/{id}")]
public async Task<ActionResult<Member>> GetMember(int id)
string Baseurl = "https://localhost:5001/";
string name = "something";
string password = "somethingelse";
id = 12121210;
Member member = new Member();
member.Name = name;
member.Password = password;
member.ID = id;
string uri = Baseurl + id + name + "/" + password;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage Res = await client.GetAsync("/api/member/something/somethingelse/12122110");
if (Res.IsSuccessStatusCode)
{
var MemResponse = Res.Content.ReadAsStringAsync().Result;
member = JsonConvert.DeserializeObject<Member>(MemResponse);
}
return Ok(member);
}
}
上面的方法没有问题,并返回JSON字符串。但是,当我替换下面的行
HttpResponseMessage Res = await
client.GetAsync("/api/member/something/somethingelse/12122110");
关注
string uri = Baseurl + id + name + "/" + password;
HttpResponseMessage Res = await client.GetAsync(uri);
我得到以下
{StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content:
System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: {
Cache-Control: private Server: Microsoft-IIS/10.0 X-SourceFiles: =?UTF-8?
B?SDpcQVRQLkZPX1NDX0NHSVxBVFBGT1NlcnZpY2U=?= X-Powered-By: ASP.NET Date:
Thu, 14 Feb 2019 16:49:04 GMT Content-Type: text/html; charset=utf-8
Content-Length: 5390 }}
我在做什么错?这是我的第一个Web API应用程序,我不确定我是否完全了解规则。
答案 0 :(得分:0)
我实际上解决了这个问题。这是方法的参数,它是一个整数。
public async Task<ActionResult<Member>> GetMember(string id)
将其更改为
{{1}}
现在一切正常。