zoukankan      html  css  js  c++  java
  • 缩短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=***

  • 相关阅读:
    .net2.0播放WAV文件
    WEB打印:目前的几种方式及我们的任务[转]
    ASP.NET MVC Framework CTP版本将于今年年底放出[转CSDN]
    最安全的写日志方法
    学习Asp.net最有价值的网站
    【资源共享】《DDR常见问题简单排查》
    【资源共享】《Camera_for_RockChipSDK参考说明_v4.1》下载
    资源共享——《嵌入式Linux应用开发完全手册》韦东山 PDF电子档下载
    FireflyRK3288开发板Android编译环境搭建开荒
    【资源下载】分享个嵌入式开发的入门教程(包含视频)
  • 原文地址:https://www.cnblogs.com/xuwendong/p/6286980.html
Copyright © 2011-2022 走看看