コード例 #1
0
        public async Task <IActionResult> Index(WebAddress wa)
        {
            AjaxResponse ajaxResponse = new AjaxResponse();

            ajaxResponse.url = wa.Url;
            try
            {
                if (!ModelState.IsValid)
                {
                    ajaxResponse.htmlResult = "<li>The URL field is not a valid fully-qualified http, https, or ftp URL.</li>";
                    return(Content(JsonConvert.SerializeObject(ajaxResponse)));
                }

                wa.Url           = UrlFormater.GetLeftPart(wa.Url);
                ajaxResponse.url = wa.Url;

                if (db.WebAddresses.Where(w => w.Url == wa.Url).Any())
                {
                    ajaxResponse.htmlResult = "<li>This url has already checked.</li>";
                    return(Content(JsonConvert.SerializeObject(ajaxResponse)));
                }
                var sitemap = new Sitemap {
                    SitemapLink = "-",
                    Pages       = new List <Page>()
                };
                sitemap.Pages.Add(new Page {
                    PageLink = "", Sitemap = sitemap
                });

                if (wa.Sitemaps == null)
                {
                    wa.Sitemaps = new List <Sitemap>();
                }

                SitemapSearcher searcher = new SitemapSearcher(wa);
                searcher.FindIn("robots.txt");
                wa = searcher.webAddress;

                if (wa.isSuccess)
                {
                    LinkParser ls = new LinkParser(searcher.webAddress);
                    wa = await ls.GetLinksAsync(wa);
                }

                ajaxResponse.htmlResult += "<li>Discovered: " + wa.Sitemaps.Count + " sitemaps</li>";


                wa.Sitemaps.Add(sitemap);
                db.WebAddresses.Add(wa);
                await db.SaveChangesAsync();

                ajaxResponse.isSuccess   = true;
                ajaxResponse.htmlResult += "<li>Discovered: " + db.Pages.Where(p => p.Sitemap.UId == wa.UId).Count() + " pages from sitemaps</li>";
                ajaxResponse.htmlResult += "<li>Starting to checking links...</li>";
            }
            catch
            {
                ajaxResponse.isSuccess  = false;
                ajaxResponse.htmlResult = "<li>Server error</li>";
            }

            return(Content(JsonConvert.SerializeObject(ajaxResponse)));
        }