Beispiel #1
0
        public ActionResult EditPost(UrlShortnerViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                else
                {
                    long UrlId = (long)TempData["UrlId"];
                    Database.Entities.UrlShortner objUrlShortner = urlShortnerRepository.GetUrlById1(UrlId);
                    objUrlShortner.UpdatedDate = DateTime.Now.Date;
                    objUrlShortner.UpdatedBy   = Convert.ToInt64(Session["UserId"]);
                    objUrlShortner.OriginalUrl = model.OriginalUrl;
                    urlShortnerRepository.Edit(objUrlShortner);
                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (Exception ex)
            {
            }
            return(View());
        }
Beispiel #2
0
        public ActionResult Create(UrlShortnerViewModel model, FormCollection form)
        {
            string SelectedAdId = form["SelectedAd"];
            long   J;
            //string requesturl = HttpContext.Request.Url.AbsoluteUri.ToString();
            var requesturl = string.Format("{0}://{1}{2}", HttpContext.Request.Url.Scheme,
                                           HttpContext.Request.Url.Authority,
                                           Url.Content("~"));

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                //FTP Folder name. Leave blank if you want to upload to root folder.
                string ftpFolder = "Ad/";

                string  urlKey = "";
                dynamic shorturlList;

                if (model.OriginalUrl.StartsWith("http://") || model.OriginalUrl.StartsWith("https://"))
                {
                }
                else
                {
                    model.OriginalUrl = "http://" + model.OriginalUrl;
                }

                //create short url key
                do
                {
                    urlKey       = GetURL();
                    shorturlList = urlShortnerRepository.GetAllShortUrl(hosturl);
                }
                //check if already exist then create again short key
                while (shorturlList.Contains(urlKey));

                var strReturnHTMLGenerate = RenderViewToString("AdViewClone", "", ControllerContext);

                string HTMLFileUrl = "";

                if (!string.IsNullOrEmpty(SelectedAdId))
                {
                    HTMLFileUrl = hosturl + "/AdHTML/" + adManageRepository.GetFileName(Int64.Parse(SelectedAdId.ToString()));
                }
                else
                {
                    HTMLFileUrl = hosturl + "/AdHTML/" + adManageRepository.GetFileName(adManageRepository.MemberAdId(Int64.Parse(Session["UserId"].ToString())));
                }

                string HTMLAdViewUrl = hosturl + "/Ad/" + urlKey + ".html";

                strReturnHTMLGenerate = strReturnHTMLGenerate.Replace(hosturl + "AdHTML/ad.html", HTMLFileUrl);
                strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("http://www.example.com", model.OriginalUrl);
                strReturnHTMLGenerate = strReturnHTMLGenerate.Replace("https://www.google.co.in", model.OriginalUrl);

                //Read the FileName and convert it to Byte array.

                try
                {
                    string directorypath = "";
                    if (requesturl.Contains(domain_ijli))
                    {
                        directorypath = @"C:\Inetpub\vhosts\ijl.li\httpdocs\UrlShotner\" + ftpFolder + urlKey + ".html";
                    }
                    else
                    {
                        directorypath = @"C:\Inetpub\vhosts\aiim.li\httpdocs\UrlShotner\" + ftpFolder + urlKey + ".html";
                    }
                    //using (FileStream fs = new FileStream(Server.MapPath("~\\Ad\\" + urlKey + ".html"), FileMode.Create))
                    using (FileStream fs = new FileStream(directorypath, FileMode.Create))
                    {
                        using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                        {
                            w.Write(strReturnHTMLGenerate);
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(View(model));
                    //throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
                }

                Database.Entities.UrlShortner objUrlShortner = new Database.Entities.UrlShortner();
                objUrlShortner           = Mapper.Map <UrlShortnerViewModel, Database.Entities.UrlShortner>(model);
                objUrlShortner.AddedDate = DateTime.Now.Date;
                objUrlShortner.RegisId   = long.TryParse(Session["UserId"].ToString(), out J) == true?Int64.Parse(Session["UserId"].ToString()) : 0;

                objUrlShortner.UpdatedDate = DateTime.Now.Date;
                //objUrlShortner.ShortUrlKey = ConfigurationManager.AppSettings["HostUrl"] + GetURL();
                objUrlShortner.ShortUrlKey = hosturl + urlKey;
                objUrlShortner.UpdatedBy   = Int64.Parse(Session["UserId"].ToString());

                //single user or admin
                if ((string)Session["MemberShipType"] == "Single" || IsAdmin(Int64.Parse(Session["UserId"].ToString())))
                {
                    objUrlShortner.AdId = adManageRepository.MemberAdId(Int64.Parse(Session["UserId"].ToString()));
                }

                urlShortnerRepository.AddUrl(objUrlShortner);
                bool result = urlShortnerRepository.SaveAll();

                if (result == true)
                {
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    return(View(model));
                }
            }
        }