/// <summary> /// This required method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// </summary> /// <param name="app">IApplicationBuilder</param> /// <param name="env">IWebHostEnvironment</param> /// <param name="logger">ILogger<Startup></param> /// <param name="lifetime">IHostApplicationLifetime</param> /// <param name="dbContext">CDCavellDbContext</param> /// <method>Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger, IHostApplicationLifetime lifetime, CDCavellDbContext dbContext)</method> public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger <Startup> logger, IHostApplicationLifetime lifetime, CDCavellDbContext dbContext) { _logger = new Logger(logger); _logger.Trace($"Configure(IApplicationBuilder: {app}, IWebHostEnvironment: {env}, ILogger<Startup> {logger}, IHostApplicationLifetime: {lifetime})"); AESGCM.Seed(_appSettings.SecretKey); DbInitializer.Initialize(dbContext); new Sitemap(_logger, _webHostEnvironment, _appSettings).Create(dbContext); lifetime.ApplicationStarted.Register(OnAppStarted); lifetime.ApplicationStopping.Register(OnAppStopping); lifetime.ApplicationStopped.Register(OnAppStopped); app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseExceptionHandler("/Home/Error/500"); app.UseStatusCodePagesWithRedirects("~/Home/Error/{0}"); app.UseXMLSitemap(env.ContentRootPath); if (env.EnvironmentName.Equals("Production")) { app.UseHsts(); app.UseHttpsRedirection(); } app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = ctx => { const int durationInSeconds = 60 * 60 * 24 * 365; ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds; } }); app.UseCookiePolicy(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); }); }
/// <summary> /// Create sitemap.xml in ASP.NET Core /// <br /><br /> /// https://www.c-sharpcorner.com/article/create-and-configure-sitemap-xml-in-asp-net-core/ /// </summary> /// <method>public void Create()</method> public void Create(CDCavellDbContext dbContext) { Assembly asm = Assembly.GetExecutingAssembly(); var controllerActionList = asm.GetTypes() .Where(type => typeof(Controller).IsAssignableFrom(type)) .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)) .Select(x => new { Controller = x.DeclaringType.Name, Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", string.Empty))) }) .ToList() .Where(x => x.Attributes.Contains("AllowAnonymous") && x.Attributes.Contains("HttpGet")) .Where(x => !x.Controller.Equals("AccountController")) .Where(x => !x.Controller.Equals("HomeController") || !x.Action.Equals("WithdrawConsent")) .ToList(); var url = "https://cdcavell.name"; var list = new List <SitemapNode>(); list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.9, Url = url, Frequency = SitemapFrequency.Daily }); foreach (var controllerAction in controllerActionList) { if (controllerAction.Controller.Equals("HomeController") && controllerAction.Action.Equals("Revoke")) { list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.6, Url = url + "/" + controllerAction.Controller.Replace("Controller", string.Empty) + "/" + controllerAction.Action + "?provider=microsoft", Frequency = SitemapFrequency.Yearly }); list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.6, Url = url + "/" + controllerAction.Controller.Replace("Controller", string.Empty) + "/" + controllerAction.Action + "?provider=google", Frequency = SitemapFrequency.Yearly }); list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.6, Url = url + "/" + controllerAction.Controller.Replace("Controller", string.Empty) + "/" + controllerAction.Action + "?provider=github", Frequency = SitemapFrequency.Yearly }); list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.6, Url = url + "/" + controllerAction.Controller.Replace("Controller", string.Empty) + "/" + controllerAction.Action + "?provider=twitter", Frequency = SitemapFrequency.Yearly }); list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.6, Url = url + "/" + controllerAction.Controller.Replace("Controller", string.Empty) + "/" + controllerAction.Action + "?provider=facebook", Frequency = SitemapFrequency.Yearly }); } else { list.Add(new SitemapNode { LastModified = DateTime.UtcNow, Priority = 0.7, Url = url + "/" + controllerAction.Controller.Replace("Controller", string.Empty) + "/" + controllerAction.Action, Frequency = SitemapFrequency.Daily }); int count = SiteMap.GetCount(controllerAction.Controller.Replace("Controller", string.Empty), controllerAction.Action, dbContext); if (count == 0) { SiteMap siteMap = new SiteMap(); siteMap.Controller = controllerAction.Controller.Replace("Controller", string.Empty); siteMap.Action = controllerAction.Action; siteMap.AddUpdate(dbContext); } } } new SitemapDocument().CreateSitemapXML(list, _webHostEnvironment.ContentRootPath); BingWebmaster bingWebmaster = new BingWebmaster(_appSettings.Application.BingWebmaster.ApiKey); UrlSubmissionQuota quota = bingWebmaster.GetUrlSubmission(url).Result; var siteMaps = SiteMap.GetNotSubmittedSiteMap(dbContext); foreach (SiteMap siteMap in siteMaps) { if (quota.DailyQuota > 0 && quota.MonthlyQuota > 0) { HttpStatusCode statusCode = bingWebmaster.SubmitUrl(url, url + "/" + siteMap.Controller + "/" + siteMap.Action).Result; if (statusCode == HttpStatusCode.OK) { siteMap.LastSubmitDate = DateTime.Now; siteMap.AddUpdate(dbContext); } quota = bingWebmaster.GetUrlSubmission(url).Result; } } using (var googleClient = new HttpClient()) { HttpResponseMessage response = googleClient.GetAsync("https://www.google.com/ping?sitemap=" + url + "/sitemap.xml").Result; if (!response.IsSuccessStatusCode) { _logger.Warning( "Google Sitemap Ping: " + response.StatusCode.ToString() + " [" + response.ReasonPhrase + "]" ); } } }