定位到URL时,本地路由不起作用应返回JSON [英] localhost route not working should be returning JSON back when locating to URL

查看:117
本文介绍了定位到URL时,本地路由不起作用应返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序员同伴,我基本上有一个异步Get()方法,该方法可以成功读取Json数据,但是当定位到路由-> localhost:59185/api/encompass/data时,我会收到一条消息:

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>

我非常希望它返回我的JSON,尤其是在调试代码时,它位于底部的'string res'中

I was very hopeful that it would return my JSON especially when in debug the code its sitting in 'string res' at the bottom

任何人都知道为什么不返回的Json甚至认为它位于'res'中吗?

anyone know why its not returning Json even thought its sitting in 'res'?

控制器:

    [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 
        }

    }

推荐答案

常见的问题,在Get方法的标头中使用的承载令牌必须经过Base64编码

fay, the bearer token used in the header of your Get method must be Base64 encoded

请参阅[如何对base64字符串进行编码和解码] [1]

See [how-do-i-encode-and-decode-a-base64-string][1]

我为令牌添加了Bearer HEADER值.

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;
        }

这篇关于定位到URL时,本地路由不起作用应返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆