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

Rob Kemp 0 Reputation points

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

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.


// Configure the HTTP request pipeline.
//if (!app.Environment.IsDevelopment())


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


    //Added class attributes and PageModel inheritance
    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    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.

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

2 answers

Sort by: Most helpful
  1. Bruce ( 55,196 Reputation points

    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:


    then create a razor page error page, and change the


    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,876 Reputation points Microsoft Vendor

    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

    var app = builder.Build();

    4.Add the error handler:

    0 comments No comments