码迷,mamicode.com
首页 > Windows程序 > 详细

webapi Action中跳转到指定内容

时间:2021-02-26 13:03:43      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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("</br>");
            }
            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($"<a href=‘http://www.baidu.com‘>{ticket}</a>", 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!