Are handler methods technically optional in a Razor Pages PageModel? #66078
Replies: 1 comment
|
Yes—handler methods are optional, but the precise behavior is not that ASP.NET Core generates a hidden The Razor Pages invoker does two relevant things:
You can see that directly in So a GET request to a Razor Page whose Your named handlers are also valid Razor Pages handlers. For example: Returning That can be reasonable for a small page-specific read operation, but I would not use it as the general REST API architecture. Page handlers are selected after the page itself has been routed, so they cannot each define an independent route or use HTTP method route attributes. ASP.NET Core documents that limitation here: Minimal APIs or API controllers give each operation an explicit route and HTTP method and fit status codes, content negotiation, authorization policies, and OpenAPI much more naturally. They also keep the API contract independent from the UI page lifecycle. In short:
Also avoid using GET handlers for state-changing operations; use the appropriate non-GET verb and apply antiforgery protection where browser cookie authentication is involved. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I recently had to add some makeshift API endpoints to a Razor Pages app that I could call from any other page on the site using client-side JS. I found an article that achieved this by adding a new empty Razor content page with no HTML, and a backing PageModel with some handler methods to serve as API endpoints. I went with this approach for now to quickly get a working MVP until I can learn more about .NET Core's Web APIs.
While it has turned out great for my MVP, it has kinda gone against my existing understanding of how Razor Pages works. I thought that a Razor Page needed to have an
OnGet()orOnGetAsync()default handler method at minimum to work, either with avoidreturn type to implicitly return the associated Razor content page or to explicitly return aPageResultobject.I also thought that a Razor Page must have some HTML associated with it in the associated
.cshtmlfile, but it looks like that's not the case, given that my handler API endpoints work just fine returningJsonResultobjects and in the routing system. It makes me curious if I could technically implement a front-facing REST API in this way (I wouldn't actually do this, but would it work?).I tested out adding some basic HTML to a random Razor content page and having a PageModel without any handler methods defined (a completely empty class), and when I navigated to the URL for that page the HTML for it still loaded. So it looks like by default, an
OnGet()handler method is defined for you that implicitly returns the associated Razor content page? Unless I override it with a definition of my own, it uses this default? Is that accurate?My code looks something like this for reference:
My Razor content page:
My backing PageModel class:
Appreciate anyone who is able to provide clarity on this.
All reactions