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;
            }
  • 相关阅读:
    为什么要做服务化?
    同步 互斥锁 读写锁 区别
    http request header 中的host行的作用
    关于group by 、group by having、where group by与 group by order by
    where,having与 group by连用的区别
    数据库:drop、truncate、delete三者删除的区别
    设置 TOMCAT 请求超时时间 和 最大连接数
    访问平台Servlet时,如何设置超时时间
    开源一款简单清爽的日历组件,JavaScript版的
    关于响应式布局
  • 原文地址:https://www.cnblogs.com/LittleJin/p/14447340.html
Copyright © 2011-2022 走看看