之前给合作方二维码时隐藏的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, );
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 >= )
{
result = Number[(int)(n % l)] + result;
n /= l;
}
result = Number[(int)n] + result;
return result;
} ///
/// 还原ID标识
///
///
///
public static long UnShort(string s)
{
long result = ;
s = s.Trim();
int l = s.Length;
int m = Number.Length;
for (int x = ; x < l; x++)
{
result += Number.IndexOf(s[l - - 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=***

缩短url-url短地址链接的更多相关文章

  1. URL压缩算法的短地址

    时下,短网址应用已经在全国各大微博上開始流行了起来.比如QQ微博的url.cn,新郎的t.cn等. 我们在新浪微博上公布网址的时候.微博会自己主动判别网址.并将其转换,比如:http://t.cn/h ...

  2. C#如何实现url短地址?C#短网址压缩算法与短网址原理入门

    c# url短地址压缩算法与短网址原理的例子,详细介绍了短网址的映射算法,将长网址md5生成32位签名串,分为4段,每段8个字节,然后生成短网址,具体见文本实例. 短网址映射算法: 将长网址md5生成 ...

  3. 【转】C# URL短地址压缩算法及短网址原理解析

    这篇文章主要介绍了C# URL短地址压缩算法及短网址原理解析,本文重点给出了算法代码,需要的朋友可以参考下 短网址应用已经在全国各大微博上开始流行了起来.例如QQ微博的url.cn,新郎的sinaur ...

  4. URL短地址压缩算法 微博短地址原理解析 (Java实现)

    原博客地址:http://blog.csdn.net/xyz_lmn/article/details/8057270 最近,项目中需要用到短网址(ShortUrl)的算法,于是在网上搜索一番,发现有C ...

  5. 【转】URL短地址压缩算法 微博短地址原理解析 (Java实现)

    转自: URL短地址压缩算法 微博短地址原理解析 (Java实现) 最近,项目中需要用到短网址(ShortUrl)的算法,于是在网上搜索一番,发现有C#的算法,有.Net的算法,有PHP的算法,就是没 ...

  6. url.cn短网址批量缩短开发接口

    https://www.showapi.com/api/view/1728 //md5签名方式--非简单签名 <?php header("Content-Type:text/html; ...

  7. 用PHP实现URL转换短网址的算法示例

    短网址就是把一个长的地址转换在超级短的网址,然后访问短网址即可跳转到长网址了,下面来看用PHP实现URL转换短网址的算法与例子. 短网址(Short URL) ,顾名思义就是在形式上比较短的网址.在W ...

  8. mvc中多参数URL会很长,首次加载不传参数让url很短,路由规则实现方法[bubuko.com]

    如要实现列表中地址全路径“bubuko-11-2.html”,在首次进入时,使用短路径“bubuko.html”,只有再次href后才显示全路径“bubuko-11-2.html”,下面使用路由规则来 ...

  9. 爪哇国新游记之二十八----从url指定的地址下载文件到本地

    package download; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; ...

随机推荐

  1. subversion-fundamental concepts

    1.在使用svn 的时候,版本号version的问题一直困恼. 如在目录/app/controller上面右键选择查看该目录的 show log ,弹出的窗口显示的Revision log.单击每一条 ...

  2. Object-C 自学笔记 - 1

    1.基本变量类型 类型 标示符 输出格式 整形 int %i 浮点 float %f 双精度 double %g 单字符 char %c 以上是基本类型,除此之外还有long, long long i ...

  3. st-Spanning Tree

    st-Spanning Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...

  4. css 重新学习系列(2)

    摘自: http://www.cnblogs.com/liuzhaoyang/articles/3289456.html Position定位:relative | absolute 定位一直是WEB ...

  5. gSoap工具wsdl2h及soapcpp2指令汇总

    gSoap开发包的下载地址http://sourceforge.net/projects/gsoap2,在bin目录下提供了两个工具: 1:wsdl2h:The gSOAP wsdl2h tool i ...

  6. SqlMapClient ,SqlExecutor 和SqlMapClientTemplate 的区别?

    SqlMapClient SqlExecutor SqlMapClientTemplate

  7. - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法浅析

    转载自:http://blog.csdn.net/ronaldo_carry/article/details/49070119 将viewdidload里面的代码全部注释掉 - (void)viewD ...

  8. 前端知识复习一(css)

    1.清楚浮动 父盒子高度为0,子盒子全部定位.浮动.子盒子不会撑开父盒子,下面的盒子会顶上来 清楚方法: clear:both: overflow:hidden: 加空标签 单/双 //双标签 .cl ...

  9. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  10. FragmentTabHost使用注意

    FragmentTabHost使用时每次切换回Fragment时,都会再走一遍onCreateView,解决办法是缓存View,具体如下 private View rootView;//缓存Fragm ...