You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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() or OnGetAsync() default handler method at minimum to work, either with a void return type to implicitly return the associated Razor content page or to explicitly return a PageResult object.
I also thought that a Razor Page must have some HTML associated with it in the associated .cshtml file, but it looks like that's not the case, given that my handler API endpoints work just fine returning JsonResult objects 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:
@page
@model TestRazorPagesApp.Pages.Test
My backing PageModel class:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace TestRazorPagesApp.Pages;
[Authorize]
public class Test : PageModel
{
public IActionResult OnGetXDataObj()
{
return new JsonResult(data);
}
public IActionResult OnGetYDataObj()
{
return new JsonResult(data);
}
}
Appreciate anyone who is able to provide clarity on this.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
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.
Beta Was this translation helpful? Give feedback.
All reactions