问题
My fellow programmers, I basically have this async Get() method which is reading Json data successfully, but when locating to route -> localhost:59185/api/encompass/data I receive a message:
No HTTP resource was found that matches the request URL 'http://localhost:59185/api/encompass/data'.
</Message>
I was very hopeful that it would return my JSON especially when in debug the code its sitting in 'string res' at the bottom
anyone know why its not returning Json even thought its sitting in 'res'?
Controller:
[HttpGet, Route("encompass/data")]
public async Task<string> Get(string Accesstoken)
{
string res = "";
using (var client = new HttpClient())
{
Accesstoken = Accesstoken.Substring(17, 28);
client.BaseAddress = new Uri("https://api.elliemae.com/");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);
var response = client.GetAsync("encompass/v1/loans/ea7c29a6-ee08-4816-99d2-fbcc7d15731d").Result;
using (HttpContent content = response.Content)
{
// ... Read the string.
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}
return res; //<- this is not returning the JSon thats sitting in here
}
}
回答1:
fay, the bearer token used in the header of your Get method must be Base64 encoded
See [how-do-i-encode-and-decode-a-base64-string][1]
I have added a Bearer HEADER value for the token.
[HttpGet, Route("values/get")]
public async Task<string> Get(string resulted)
{
string res = "";
using (var client = new HttpClient())
{
// HTTP POST
client.BaseAddress = new Uri("https://api.elliemae.com/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(resulted)));
var response = client.GetAsync("/encompass/v1/loans/{ea7c29a6-ee08-4816-99d2-fbcc7d15731d}?Authorization=Bearer "+resulted+"&Content-Type=application/json").Result;
using (HttpContent content = response.Content)
{
// ... Read the string.
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}
}
return res;
}
来源:https://stackoverflow.com/questions/56531267/localhost-route-not-working-should-be-returning-json-back-when-locating-to-url