Ejemplo n.º 1
0
    public void btnShorten2_Click(object sender, EventArgs e)
    {
        if (txtLongUrl.Text != "" && IsValidUri(txtLongUrl.Text) != false)
        {
            string baseUrl    = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
            string shortenKey = RandomString(6);
            string finalURL   = baseUrl + shortenKey;

            LearnEFEntities  db            = new LearnEFEntities();
            tbl_shortenedUrl shortnDetails = new tbl_shortenedUrl();
            shortnDetails.longUrl  = txtLongUrl.Text;
            shortnDetails.shortUrl = finalURL;
            shortnDetails.urlKey   = shortenKey;
            shortnDetails.addedOn  = DateTime.Now;

            db.tbl_shortenedUrl.Add(shortnDetails);
            db.SaveChanges();
            //Console.Write(finalURL);
            lblShortenedUrl.InnerText = finalURL;
            btnCopy.Visible           = true;
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please specify a valid url!')", true);
        }
    }
Ejemplo n.º 2
0
    public void btnShorten3_Click(object sender, EventArgs e)
    {
        string baseUrl   = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
        string customKey = txtShortCustom.Text;

        if (txtLongUrl.Text != "")
        {
            if (customKey != "" && customKey.Length <= 6 && System.Text.RegularExpressions.Regex.IsMatch(customKey, @"^[a-zA-Z0-9]+$"))
            {
                if (!IsExists(customKey))
                {
                    string           finalURL      = baseUrl + customKey;
                    LearnEFEntities  db            = new LearnEFEntities();
                    tbl_shortenedUrl shortnDetails = new tbl_shortenedUrl();
                    shortnDetails.longUrl  = txtLongUrl.Text;
                    shortnDetails.shortUrl = finalURL;
                    shortnDetails.urlKey   = customKey;
                    shortnDetails.addedOn  = DateTime.Now;
                    db.tbl_shortenedUrl.Add(shortnDetails);
                    db.SaveChanges();
                    //Console.Write(finalURL);
                    lblShortenedUrl.InnerText = finalURL;
                    //btnCopy2.Visible = true;
                    btnCopy.Visible = true;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Name has already been taken!')", true);
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please specify valid custom url!')", true);
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please specify a valid url to shorten!')", true);
        }
    }