Browse Source

重定向改进

懒得勤快 5 years ago
parent
commit
d4b77b0b26

+ 2 - 2
src/Masuit.MyBlogs.Core/Controllers/AdvertisementController.cs

@@ -30,7 +30,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="id">广告id</param>
         /// <returns></returns>
         [HttpGet("{id:int}"), ResponseCache(Duration = 3600)]
-        public async Task<IActionResult> Redirect(int id)
+        public async Task<IActionResult> RedirectTo(int id)
         {
             var ad = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("推广链接不存在");
             if (!HttpContext.Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get<string>("ads" + id)))
@@ -40,7 +40,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 await AdsService.SaveChangesAsync();
             }
 
-            return RedirectPermanent(ad.Url);
+            return Redirect(ad.Url);
         }
 
         /// <summary>

+ 4 - 5
src/Masuit.MyBlogs.Core/Controllers/ShortController.cs

@@ -1,25 +1,24 @@
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.Tools;
 using Microsoft.AspNetCore.Mvc;
-using System;
 
 namespace Masuit.MyBlogs.Core.Controllers
 {
     public class ShortController : Controller
     {
-        [HttpGet("short")]
+        [HttpGet("short"), MyAuthorize]
         public IActionResult Short(string url)
         {
-            var id = Math.Abs(url.GetHashCode()).ToBinary(62);
+            var id = url.Crc32().FromBinary(16).ToBinary(62);
             RedisHelper.Set("shorturl:" + id, url);
             return Ok(id);
         }
 
         [HttpGet("{key}")]
-        public ActionResult Redirect(string key)
+        public ActionResult RedirectTo(string key)
         {
             var url = RedisHelper.Get("shorturl:" + key) ?? throw new NotFoundException("链接未找到");
-            return RedirectPermanent(url);
+            return Redirect(url);
         }
     }
 }