dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.15k stars 9.92k forks source link

The website is called before pressing enter ASP.NET Core MVC #56971

Closed ThanhDeveloper closed 1 month ago

ThanhDeveloper commented 1 month ago

Is there an existing issue for this?

Describe the bug

I created new project with ASP.NET Core MVC latest version. It shows a strange behavior. When I launch the project and navigate to https://localhost:7266/ and add / manually after the url, the browser suggests the previous url visited. I pressed the scroll key on the keyboard to the url https://localhost:7266/email-verification without pressing the enter key. Debug will touch the code with the router above (but browser will not do anything and will not change. just hit breakpoint in my code).

Is this behavior correct? Is there any documentation about this? It would be a waste if users misuse this and affect the performance of the website.

Then pressed enter and it will be call again:

image

image

My code:

public class VerificationController(TimeProvider timeProvider) : Controller
{
    [Route("email-verification")]
    public IActionResult EmailVerification()
    {
        ViewBag.CurrentYear = timeProvider.GetUtcNow().Year;
        Console.WriteLine("Called");
        return View();
    }
}

program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddSingleton<TimeProvider, ZonedTimeProvider>();

builder.Services
    .AddDbContext<IdentityPortalDbContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("Sql")));

var app = builder.Build();

if (builder.Configuration.ShouldMigrateDatabases())
{
    await app.MigrateDatabase();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

await app.RunAsync();

Expected Behavior

Breakpoints should not fire when pressing enter on the browser

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.300

Anything else?

No response

martincostello commented 1 month ago

This is probably Chrome trying to pre-fetch page metadata.

ThanhDeveloper commented 1 month ago

This is probably Chrome trying to pre-fetch page metadata.

But let's imagine that we do a lot of things before return view. Will it waste resources?

martincostello commented 1 month ago

I don't know - it depends what your application does.

If it is Chrome pre-fetching, then it has nothing to do with ASP.NET Core anyway and won't be something the team can do anything about.

Do you see same behaviour in other browsers such as Edge, Firefox, Safari etc.?

ThanhDeveloper commented 1 month ago

It seems to be happening on chrome. edge does not have this happen

I don't know - it depends what your application does.

If it is Chrome pre-fetching, then it has nothing to do with ASP.NET Core anyway and won't be something the team can do anything about.

Do you see same behaviour in other browsers such as Edge, Firefox, Safari etc.?

It seems to be happening on chrome. edge does not have this happen. Stop app and reopen again. Then paste the url on anonymous tab in Chrome and it will be hit the breakpoint !

ThanhDeveloper commented 1 month ago

It seems like this is happening because of the browser. So I will close the issue. Thank