[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 }); }