縮短url-url短地址鏈接


之前給合作方二維碼時隱藏的url過長,導致合作方提出在打印的時候打印不出來的問題,要求url長度在50字節內,所以寫了縮短url功能。

var url = string.Format("{0}/Billing/ScanCode?TenantId={1}&BussinessType={2}&groupNumber={3}&DeviceId={4}", baseUrl, args.TenantId, (int)BussinessType.SyncTransaction, groupNumber, device.Id);

//過長的url 優化成短url
var creatShotUrl = string.Format("/Billing/ScanCode?TenantId={0}&BussinessType={1}&groupNumber={2}&DeviceId={3}", args.TenantId, (int)BussinessType.SyncTransaction, groupNumber, device.Id);
var invoiceUrlRepository = RF.Concrete<InvoiceUrlRepository>();
InvoiceUrl model = new InvoiceUrl();
string id = CommonShortUrl.GetShorturl(creatShotUrl, 0);
url = baseUrl + "/t?e=" + id;

再添加一個控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rafy.Domain;
namespace DBI.SaaS.Web.Controllers
{
    public class TController : Controller
    {

        // GET: InvoiceUrl
        /// <summary>
        /// 根據短url的key 獲取 真實的url 並跳轉
        /// </summary>
        /// <param name="urlKey">短url的key</param>
        /// <returns></returns>
        [Route("c/{e:maxlength(15)}")]
        public ActionResult Index(string e)
        {
            if (!string.IsNullOrEmpty(e))
            {
                long id = CommonShortUrl.UnShort(e);
                var invoiceUrlRepository = RF.Concrete<InvoiceUrlRepository>();
                //查詢
                var q = new CommonQueryCriteria();
                var model = invoiceUrlRepository.GetById(id);
                if (model == null)
                {
                    return Content("不存在的url!");
                }
                return Redirect(model.UrlValue);
            }
            else
            {
                return Content("參數錯誤!");
            }
        }
    }
}

縮短url控制器代碼

using Rafy.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DBI.SaaS.Web.Controllers
{
    public class CommonShortUrl
    {
        static string Number = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        ///
        /// 壓縮ID標識
        ///
        ///
        ///
        public static string Short(long n)
        {
            string result = string.Empty;
            int l = Number.Length;
            while (n / l >= 1)
            {
                result = Number[(int)(n % l)] + result;
                n /= l;
            }
            result = Number[(int)n] + result;
            return result;
        }

        ///
        /// 還原ID標識
        ///
        ///
        ///
        public static long UnShort(string s)
        {
            long result = 0;
            s = s.Trim();
            int l = s.Length;
            int m = Number.Length;
            for (int x = 0; x < l; x++)
            {
                result += Number.IndexOf(s[l - 1 - x]) * (long)Math.Pow(m, x);
            }
            return result;
        }

        /// <summary>
        /// 簡化 url
        /// </summary>
        /// <param name="paramUrl"></param>
        /// <returns></returns>
        public static string GetShorturl(string paramUrl,int urlType)
        {
            //過長的url 優化成短url
            var invoiceUrlRepository = RF.Concrete<InvoiceUrlRepository>();
            InvoiceUrl insertModel = new InvoiceUrl();
            insertModel.UrlValue = paramUrl;
            insertModel.UrlType = urlType;
            invoiceUrlRepository.Save(insertModel);
            return CommonShortUrl.Short(insertModel.Id);
        }
    }
}

這樣縮短后的url地址:http://www.***.com/t?e=***


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM