缩短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=***

转载于:https://www.cnblogs.com/xuwendong/p/6286980.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值