zoukankan      html  css  js  c++  java
  • webapi Action中跳转到指定内容

    最近项目里面有用到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;
            }
  • 相关阅读:
    HashMap实现原理
    设计模式-2-代理模式
    设计模式-1-单例模式
    重构技巧
    unable to create new native thread
    设计模式-6大原则
    阿里前端在线编程题
    如何实现用户懒加载?
    《我敢活成我想要的样子》读后感
    改bug心得
  • 原文地址:https://www.cnblogs.com/LittleJin/p/14447340.html
Copyright © 2011-2022 走看看