/// <summary> /// Index Get Method. Its return Index view /// </summary> /// <param name="id"></param> public ActionResult Index(string id) { // check id is empty or not, if not empty then search long url from database. // then redirect to long url if (!string.IsNullOrEmpty(id)) { if (!db.tblSUrls.Any(x => x.ShortUrl == id)) { return RedirectToAction("Error"); } else { tblSUrl longUrl = new tblSUrl(); longUrl = db.tblSUrls.First(x => x.ShortUrl == id); return Redirect(longUrl.LongUrl); } } ViewBag.message = "welcome back, Guest"; return View(); }
/// <summary> /// Deprecated Method for adding a new object to the tblSUrls EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddTotblSUrls(tblSUrl tblSUrl) { base.AddObject("tblSUrls", tblSUrl); }
/// <summary> /// Create a new tblSUrl object. /// </summary> /// <param name="sUrl_ID">Initial value of the SUrl_ID property.</param> /// <param name="longUrl">Initial value of the LongUrl property.</param> /// <param name="shortUrl">Initial value of the ShortUrl property.</param> public static tblSUrl CreatetblSUrl(global::System.Int32 sUrl_ID, global::System.String longUrl, global::System.String shortUrl) { tblSUrl tblSUrl = new tblSUrl(); tblSUrl.SUrl_ID = sUrl_ID; tblSUrl.LongUrl = longUrl; tblSUrl.ShortUrl = shortUrl; return tblSUrl; }
public ActionResult ShortUrlReturn(string longurl) { // shortUrlid variable is used for saving randomly generated short url string shortUrlid = null; // check longurl parameter is empty or not. // if longurl parameter is empty then return to Index view with ViewBag message if (string.IsNullOrEmpty(longurl)) { ViewBag.error = "Please, enter your long url"; return View(); } // else check long url is valid or not. // if long url is not valid then return to Index view with ViewBag message else { if(!IsUrlValid(longurl)) { ViewBag.error = "Please, enter a valid url"; return View(); } if (db.tblSUrls.Any( x => x.LongUrl == longurl)) { tblSUrl searchLongUrl = new tblSUrl(); searchLongUrl = db.tblSUrls.First(x => x.LongUrl == longurl); ViewBag.success = "Your short url " + ConfigurationManager.AppSettings["WebsiteUrl"] + "/" + searchLongUrl.ShortUrl; return View(); } } // saving randomly generated short url shortUrlid = shortUrl(); // check short url already contains in the database or not if (db.tblSUrls.Any(x => x.ShortUrl == shortUrlid)) { // if short url already contains in the databse // then its re-generated new short url shortUrlid = shortUrl(); } // if firstly generated short url is not present in the database // then long url and short url save in the database // and initialize ViewBag message else { // create a object for SUrl table tblSUrl singleSUrl = new tblSUrl(); // initialize long url singleSUrl.LongUrl = longurl; // initialize short url singleSUrl.ShortUrl = shortUrlid; // save long url and short url into SUrl table db.tblSUrls.AddObject(singleSUrl); db.SaveChanges(); ViewBag.success = "Your short url " + ConfigurationManager.AppSettings["WebsiteUrl"] + "/" + shortUrlid; } // return Index View return View(); }