webapi Action中跳转到指定内容

2021-03-01 00:27

阅读:547

标签:res   current   callback   bapi   html   read   dhtml   for   director   

最近项目里面有用到webapi接口作为回调页面,然后由页面展示回调内容

 

        [HttpGet]
        [ActionName("ScanQRCodeCallBack")]
        public System.Net.Http.HttpResponseMessage ScanQRCodeCallBack(string ticket)
        {

            //读取所有cookie
            StringBuilder sb = new StringBuilder();
            foreach (var item in this.Request.Headers)
            {
                sb.Append(($"key:{item.Key} value:{item.Value.ToArray()[0]}"));
                sb.Append("");
            }
            sb.Append("key:ticket value:" + ticket);
            string cookieStr = sb.ToString();

            Console.WriteLine(cookieStr);
            //重定向到指定页面
            var requestUri = this.Request.RequestUri;
            string newUrl = $"{requestUri.Scheme}://{requestUri.Authority}/webui/index.html{requestUri.Query}";
            //直接跳转
            HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.Moved);
            resp.Headers.Location = new Uri(newUrl);
            //resp.Headers.Add("Set-Cookie", cookieStr);
            return resp;



            //测试 直接返回cookie页面
            var cookie = new System.Net.Http.StringContent(sb.ToString());
            cookie.Headers.ContentType.MediaType = "text/html";
            System.Net.Http.HttpResponseMessage cookieMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
            cookieMessage.Content = cookie;
            return cookieMessage;


            //测试 返回指定字符串
            string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "webui", "index.html");
            string readHtml = System.IO.File.ReadAllText(path, Encoding.UTF8);
            var rValue = new System.Net.Http.StringContent(readHtml);
            //var rValue = new System.Net.Http.StringContent($"http://www.baidu.com‘>{ticket}", Encoding.UTF8, "text/html");
            rValue.Headers.ContentType.MediaType = "text/html";
            System.Net.Http.HttpResponseMessage msg = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
            msg.Headers.Add("Set-Cookie", "aa=xxxx");
            msg.Content = rValue;
            return msg;
        }

 

webapi Action中跳转到指定内容

标签:res   current   callback   bapi   html   read   dhtml   for   director   

原文地址:https://www.cnblogs.com/LittleJin/p/14447340.html


评论


亲,登录后才可以留言!