2

I'm working through Adam Freeman's book "Pro ASP.Net Core 3" (8th edition). I am running an example of a page that uses information in a database. The Configure() method in my Startup class includes an endpoint for "/sum/{count:1000000000}". The base URL is http://localhost:5000. In Google Chrome, I type "http://localhost:5000/sum", and as soon as I type the "m", the web page is requested. If I want to get the calculation for 10 by requesting http://localhost:5000/sum/10, the page for 1000000000 is requested first. I can imagine that in a real-world application, that could end up being a problem. How do I avoid that?

Here's my startup file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Platform.Services;
using Microsoft.EntityFrameworkCore;
using Platform.Models;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;

namespace Platform {
    public class Startup {

        public Startup(IConfiguration config) {
            Configuration = config;
        }

        private IConfiguration Configuration { get; set; }

        public void ConfigureServices(IServiceCollection services) {
            services.AddDistributedSqlServerCache(opts => 
            {
                opts.ConnectionString
                    = Configuration["ConnectionStrings:CacheConnection"];
                opts.SchemaName = "dbo";
                opts.TableName = "DataCache";
            });
            services.AddResponseCaching();
            services.AddSingleton<IResponseFormatter, HtmlResponseFormatter>();

            services.AddDbContext<CalculationContext>(opts =>
            {
                opts.UseSqlServer(Configuration["ConnectionStrings:CalcConnection"]);
            });
            services.AddTransient<SeedData>();
        }

        public void Configure(IApplicationBuilder app,
                IHostApplicationLifetime lifetime, IWebHostEnvironment env,
                SeedData seedData) {
            app.UseDeveloperExceptionPage();
            app.UseResponseCaching();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(endpoints => {

                endpoints.MapEndpoint<SumEndpoint>("/sum/{count:int=1000000000}");
                endpoints.MapGet("/", async context => {
                    await context.Response.WriteAsync("Hello World!");
                });
            });

            bool cmdLineInit = (Configuration["INITDB"] ?? "false") == "true";
             if (env.IsDevelopment() || cmdLineInit) {
                seedData.SeedDatabase();
                if (cmdLineInit) {
                    lifetime.StopApplication();
                }
            }
        }
    }
}
ROBERT RICHARDSON
  • 2,077
  • 4
  • 24
  • 57
  • 1
    This is probably your browser trying to make it look like it is loading pages faster, by starting requests as soon as it sees something that looks like it could be a valid request. It shouldn't really be a problem in real world usage, but it could make debugging a pain for you. – Bradley Uffner Sep 25 '20 at 20:54
  • 1000000000 is default value, and it is hitting your url with default value. Check whether first request is a HEAD request or a GET request – Amit Kumar Singh Sep 25 '20 at 20:55
  • See this question / answer for information on disabling the prefetch optimization in Chrome: https://stackoverflow.com/questions/28020184/disable-web-requests-while-typing-url – Bradley Uffner Sep 25 '20 at 20:59

0 Answers0