Unable to Debug OnGet() Method in Razor Pages - ASP.NET Core

27 views Asked by At

I'm encountering an issue where the OnGet() method in my Razor pages application is not being called when I try to debug it. Consequently, the value of certain properties, like Name, is not being updated as expected during debugging. Here's a breakdown of my setup:

Problem Description

I have a Razor Page (TestModel.cshtml.cs) with an OnGet() method where I update the Name property. However, when I set breakpoints and debug the application, the OnGet() method is not called, and thus, the value of Name is not updated.

Here's a simplified version of my code:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages
{
    public class TestModel : PageModel
    {
        public string Name { get; set; }
        public string Description { get; set; }

        public void OnGet()
        {
            Name = "Test";
            Description = "This is a test name.";
        }
    }
}

And here's my Razor Page (Test.cshtml):

@page
@model WebApplication1.Pages.TestModel

<h1>Hello, @Model.Name!</h1>
<p>@Model.Description</p>`

The issue is that when I set a breakpoint in the OnGet() method and try to debug the application, the breakpoint is never hit, and consequently, the value of Name is not updated. However, during normal execution, the value is updated correctly.

I've already tried using [BindProperties] attribute, but it only works during normal execution, not during debugging.

Here are some steps I've taken to troubleshoot:

  • Checked route configuration in Startup.cs to ensure it's correctly mapping requests to my Razor Page.
  • Ensured that there are no build errors and the project is built successfully.
  • Checked for exceptions and ensured that there are no unhandled exceptions occurring before the OnGet() method gets executed.
  • Set breakpoints in the OnGet() method to ensure it's not being skipped for some reason.
  • Cleared cache/cookies and tried debugging in different browsers.

Despite these efforts, I'm still unable to debug the OnGet() method.

1

There are 1 answers

0
Md Farid Uddin Kiron On

I'm encountering an issue where the OnGet() method in my Razor pages application is not being called when I try to debug it. Consequently, the value of certain properties, like Name, is not being updated as expected during debugging.

Well based on your scenario and description, it seems that the debugging approach you have followed is correct. I have tried to investigate your issue. Based on my testing outcome, I think its not because of your code but your visual studio configuration cuasing that issue. There's might some went incorrect configuration.

I would sugegst you to, double-check your debug settings in Visual Studio. Ensure the project you want to debug is selected and the launch action is set to "IIS Express" or your preferred web server. In addition, please check this configuration in your visual studio, Suppress JIT optimization on module load should be unchecked.

enter image description here

While I was testing I have successfully, able to hit the debugger and got the expected result.

Here, is the details steps what I have done:

I have created the razor page project as following:

  1. Project Structure:

enter image description here

  1. launchSettings.json:

enter image description here

  1. program.cs

    enter image description here

Output:

enter image description here

enter image description here

Note: Please refer to this official document, for additional assistance.