zoukankan      html  css  js  c++  java
  • C#长短链接服务器端WebApi作映射

     [HttpGet]
            public IHttpActionResult GetLongLink(string code)
            {
                if (string.IsNullOrWhiteSpace(code))
                {
                    return Redirect(Request.RequestUri.AbsoluteUri.ToString() + "Home/Index");//跳转到Home/index
                }
                var longlink = Redis.GetValue(code).Replace(""", "");//取出来的带有"所以去掉它
                if (string.IsNullOrWhiteSpace(longlink))
                {
                    return Json(new { error = 1, msg = "链接已经失效" });
                }
                return Redirect(longlink);
            }
    
    
            [HttpGet]
            public IHttpActionResult GetShortLink(string longlink)
            {
                if (string.IsNullOrWhiteSpace(longlink))
                {
                    return Json(new { error = 1, msg = "长链接不能为空" });
                }
                if (longlink.IndexOf("http://") != 0 && longlink.IndexOf("https://") != 0)
                {
                    return Json(new { error = 1, msg = "长链接格式有误" });
                }
                var shortCode = Utils.ShortUrl(longlink)[0];//短网址算法计算出的值拿第一个
                Redis.SetValue(shortCode, longlink, DateTime.Now.AddDays(30));
                var shortlink = Request.RequestUri.Authority.ToString() + "/" + shortCode;//拼接完整的链接(这里不带http不过没啥影响,需要的话也可以拼上去)
    
                return Json(new { error = 0, msg = "成功", shortlink });
            }
  • 相关阅读:
    LeetCode113. 路径总和 II
    LeetCode257. 二叉树的所有路径
    LeetCode222. 完全二叉树的节点个数
    LeetCode404. 左叶子之和
    LeetCode110. 平衡二叉树
    LeetCode101. 对称二叉树
    LeetCode100. 相同的树
    llustrator CC2017下载AI2020
    vs code 代码格式化整理
    人生格言
  • 原文地址:https://www.cnblogs.com/mojiejushi/p/13534126.html
Copyright © 2011-2022 走看看