Shorten url-url short Address link

Source: Internet
Author: User

Previous to the partner QR code when the hidden URL is too long, causing the partner to ask for printing at the time of the problem, the URL length of 50 bytes, so write a shortened URL function.

varURL =string. Format ("{0}/billing/scancode? TENANTID={1}&BUSSINESSTYPE={2}&GROUPNUMBER={3}&DEVICEID={4}", BaseUrl, args. TenantId, (int) bussinesstype.synctransaction, Groupnumber, device. ID);//long URLs optimized to short URLsvarCreatshoturl =string. Format ("/billing/scancode? TENANTID={0}&BUSSINESSTYPE={1}&GROUPNUMBER={2}&DEVICEID={3}", args. TenantId, (int) bussinesstype.synctransaction, Groupnumber, device. ID);varInvoiceurlrepository = RF. Concrete<invoiceurlrepository>(); Invoiceurl model=NewInvoiceurl ();stringid = commonshorturl.getshorturl (creatshoturl,0); URL= BaseUrl +"/t?e="+ ID;

Add a second controller

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingRafy.domain;namespaceDBI. saas.web.controllers{ Public classTcontroller:controller {//Get:invoiceurl        /// <summary>        ///get the real URL based on the key of the short URL and jump/// </summary>        /// <param name= "Urlkey" >key for short URL</param>        /// <returns></returns>[Route ("c/{e:maxlength ()}")]         PublicActionResult Index (stringe) {if(!string. IsNullOrEmpty (e)) {LongID =Commonshorturl.unshort (e); varInvoiceurlrepository = RF. Concrete<invoiceurlrepository>(); //Enquiry                varQ =NewCommonquerycriteria (); varModel =Invoiceurlrepository.getbyid (ID); if(model = =NULL)                {                    returnContent ("non-existent url! "); }                returnRedirect (model.            Urlvalue); }            Else            {                returnContent ("parameter Error! "); }        }    }}

Shortening the URL controller code

usingRafy.domain;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespaceDBI. saas.web.controllers{ Public classCommonshorturl {Static stringNumber ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; ///        ///Compression ID Identification///        ///        ///         Public Static stringShort (LongN) {stringresult =string.            Empty; intL =number.length;  while(n/l >=1) {result= number[(int) (n% l)] +result; N/=l; } result= number[(int) n] +result; returnresult; }        ///        ///Restore ID Identification///        ///        ///         Public Static LongUnshort (strings) {Longresult =0; S=S.trim (); intL =s.length; intm =number.length;  for(intx =0; x < L; X + +) {result+ = Number.indexof (S[l-1-X]) * (Long) Math.pow (m, x); }            returnresult; }        /// <summary>        ///Simplify URLs/// </summary>        /// <param name= "Paramurl" ></param>        /// <returns></returns>         Public Static stringGetshorturl (stringParamurl,intUrltype) {            //long URLs optimized to short URLs            varInvoiceurlrepository = RF. Concrete<invoiceurlrepository>(); Invoiceurl Insertmodel=NewInvoiceurl (); Insertmodel.urlvalue=Paramurl; Insertmodel.urltype=Urltype;            Invoiceurlrepository.save (Insertmodel); returnCommonshorturl.short (insertmodel.id); }    }}

This shortened URL address: http://www.***.com/t?e=***

Shorten url-url short Address link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.