标签:数组 mic 设计 nta get title close doc 混合
完成一个短链接转换到存储到访问大致需要做这些工作:DB设计->Helper类->UI->路由。
1.数据库设计主要是存储短链接编码及原链接地址,其他一些插入时间等等自由设计。
2.封装一个长链接转短链接的类,这边是用了 C#生成短链接 这位博主封装的类,这个也可以自由发挥,主要是取随机数大小写字母与阿拉伯数字。
public class ShortUrlHelper { /// <summary> /// 返回六位数链接编码 /// </summary> /// <param name="url">原链接完整地址</param> /// <returns></returns> public string GetShortURL(string url) { //可以自定义生成MD5加密字符传前的混合KEY string key = DateTime.Now.ToString(); //要使用生成URL的字符 string[] chars = new string[]{ "a","b","c","d","e","f","g","h", "i","j","k","l","m","n","o","p", "q","r","s","t","u","v","w","x", "y","z","0","1","2","3","4","5", "6","7","8","9","A","B","C","D", "E","F","G","H","I","J","K","L", "M","N","O","P","Q","R","S","T", "U","V","W","X","Y","Z" }; //对传入网址进行MD5加密 string hex = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(key + url))).Replace("-", ""); string[] resUrl = new string[4]; for (int i = 0; i < 4; i++) { //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算 int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16); string outChars = string.Empty; for (int j = 0; j < 6; j++) { //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引 int index = 0x0000003D & hexint; //把取得的字符相加 outChars += chars[index]; //每次循环按位右移5位 hexint = hexint >> 5; } //把字符串存入对应索引的输出数组 resUrl[i] = outChars; } return resUrl[new Random().Next(0, 3)]; } }
3.页面可以实现用户在链接转换后默认选择文本框内容,用户直接按Ctrl+V复制即可。
@{ ViewBag.Title = "ShortURL"; } <form id="formSearch" name="formSearch" class="form-horizontal col-sm-12 col-xs-12"" action="/SmsOrder/ShortURL" style="float:left"> <div class="form-group"> <label class="control-label col-sm-2 col-xs-2" for=""><span style="color: red">*</span>原链接</label> <div class="col-xs-10 col-sm-10"> <input type="text" class="form-control" id="url" name="url" value="@ViewData["url"]"" placeholder="输入链接"> </div> </div> <div class="form-group"> <div style="text-align: center"> <input type="submit" id="btn_submit1" class="btn btn-primary" value="生成短链接"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2 col-xs-2" for=""><span style="color: red"></span>短链接</label> <div class="col-xs-10 col-sm-10"> <input type="text" class="form-control" id="shorturl" name="shorturl" value="@ViewData["shorturl"]"> </div> </div> <div class="form-group"> <div style="text-align: center; color:red"> @ViewData["msg"] </div> </div> </form> <script type="text/javascript"> window.onload = function () { var input = document.getElementById("shorturl"); if (input.value != "") { input.focus(); document.formSearch.shorturl.select(); } } </script>
后台有config配置域名、用户输入链接判断是否能成功访问等操作。
public ActionResult ShortURL(string url) { string msg = ""; string shorturl = ""; string shorturlRealmName = ConfigurationManager.AppSettings["shorturlRealmName"]; try { if (!string.IsNullOrEmpty(url)) { HttpWebResponse response = CreateRequest.CreateGetHttpResponse(url, 0, "", null); if (response.StatusCode == HttpStatusCode.OK) { Sms_URL smsurl = new Sms_URL(); smsurl.url = url; smsurl.url_code = new ShortUrlHelper().GetShortURL(url); //唯一测试 //smsurl.url_code = "YbiI3e"; smsurl.click_number = 0; smsurl.is_delete = 0; smsurl.create_time = DateTime.Now; smsurl.modify_time = DateTime.Now; bool result = _sms_URLService.AddSms_URL(smsurl); if (result) { msg = "请按Ctrl+V复制短链接框内文本"; shorturl = shorturlRealmName + smsurl.url_code; } } else { msg = "请输入有效的链接"; } } ViewData["msg"] = msg; ViewData["shorturl"] = shorturl; ViewData["url"] = url; return View(); } catch (Exception ex) { ViewData["msg"] = "请确定输入的链接有效或重新点击生成短链接"; ViewData["shorturl"] = ""; ViewData["url"] = url; return View(); } }
4.都完成之后配置一个跳转方法。这个方法放哪主要看你域名绑定的项目,我这用的是Api绑定的一个域名,手上项目因为只有api对外方便点。配置路由,因为短链接要的就是短所以只保留 域名/urlcode即可。获取code去表查询原链接,重定向原链接即可
[Route("~/{smscode}")] [HttpGet] public HttpResponseMessage smsjump(string smscode) { Sms_URL smsurl = _sms_URLService.GetSms_URLByUrlCode(smscode); var response = Request.CreateResponse(HttpStatusCode.Moved); if (smsurl == null || smsurl.is_delete == 1) return response; smsurl.click_number = smsurl.click_number + 1; _sms_URLService.UpdateSms_URL(smsurl); response.Headers.Location = new Uri(smsurl.url); return response; }
标签:数组 mic 设计 nta get title close doc 混合
原文地址:https://www.cnblogs.com/binzi/p/14034444.html