ASP.Net Core 7.0 Web App (Model-View-Controller) ErrorViewModel OnGet OnPost do not get called or executed

Rob Kemp 0 Reputation points
2024-03-27T14:09:17.93+00:00

I'm trying to follow the following article in order to implement global error handling and a custom error page for my web site.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-7.0

Running the app with IIS Express in debug.

Because I've struggled with this so much, I've started a new ASP.Net Core 7.0 Web App (Model-View-Controller) project. The setup is, at this point, I'm simply creating a new Web App project. Specifically, ASP.Net Core 7.0 Web App (Model-View-Controller). The only code I've changed from what you get by default when creating a new project is the following.

Program.cs

// Configure the HTTP request pipeline.
//if (!app.Environment.IsDevelopment())
//{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
//}

Controllers/HomeController.cs

//added HttpGet attribute to main index action.
[HttpGet]
public IActionResult Index()
{
    //throw an application exception to test error handling
    throw new ApplicationException("This is an error");
    return View();
}

Models/ErrorViewModel.cs

    //Added class attributes and PageModel inheritance
    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    [IgnoreAntiforgeryToken]
    public class ErrorViewModel : PageModel

        public string? RequestId { get; set; }

        public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);


        //added OnGet method
        public void OnGet()
        {
            string thisIsntWorking= "please work";
        }

Shared/Error.cshtml - No change. If I add @page to the top, it breaks the ViewData["Title"] usage, i.e. ViewData["Title"] = "Error" doesn't work any more. ViewData becomes null.

I feel like this should be more straight forward but I'm clearly missing something. What I'm trying to do is get whatever unhandled exception occurs in my Web App to populate to an Error Detail Web Page. I'd really like some global error handling, but I just can't seem to get the basic OnGet OnPost to work in the model and I don't know how to trickle any form of exception to the ErrorViewModel for usage by a view.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,166 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,026 Reputation points
    2024-03-27T15:58:56.5566667+00:00

    typically with MVC you would use a controller and view instead of a razor page for the error page. the default template creates a error action in the home controller, and an error view in the shared folder along with a model. this all works out of the box.

    if you want to use a razor page instead (not sure why), then add razor page support:

    app.MapRazorPages();

    then create a razor page error page, and change the

    app.UseExceptionHandler("/Home/Error");

    to use your razor page route. you probably should delete the home controller action, and shared error view.

    0 comments No comments

  2. Brando Zhang-MSFT 2,956 Reputation points Microsoft Vendor
    2024-03-28T07:38:11.3166667+00:00

    Hi @Rob Kemp,

    According to your description, it seems you want to create a custom razor page inside your MVC application.

    By default, the MVC application contains it own Error view and error action, it inside the home controller.

    User's image

    So if you just change its view to view page, it will not working, since it doesn't contain the right model.

    Then if you want to use Raozor page inside the MVC, you should add the route for it and create a new Error page:

    1.Create a new pages folder:

    2.Add a new Razor pages with the route

    User's image

    @page "/errorview"
    

    3.Add the below codes inside the program.cs to enable razor page route and service

    .....
    builder.Services.AddRazorPages();
    .....
    var app = builder.Build();
    .....
    app.MapRazorPages();
    ....
    

    4.Add the error handler:

    app.UseExceptionHandler("/errorview");
    
    0 comments No comments