Add two OnGet method in a Razor page #32064
-
Hi Team, Can we add multiple OnGetAsync in a Razor page. One might be OnGetAsync , other one might be OnGetTestAsync I am trying to avoid if else condition inside the main OnGetAsync. The route itself define which OnGet need to serve. Note: with query string its achievable but , ppl can see the handler name in the url , which i don't want . |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
CoPilot suggests: Here’s how you can achieve this: Named Handler Methods: public class MyPageModel : PageModel
} This makes the handler name part of the route, but it won’t appear in the URL unless explicitly specified. Learn more learnrazorpages.com mikesdotnetting.com stackoverflow.com talkingdotnet.com |
Beta Was this translation helpful? Give feedback.
CoPilot suggests:
Certainly! In Razor Pages, you can define multiple handler methods for a single HTTP verb using a feature called named handler methods. This allows you to specify different methods to execute based on the route without relying on query string parameters or exposing the handler name in the URL.
Here’s how you can achieve this:
Named Handler Methods:
Named handler methods allow you to define multiple methods for the same HTTP verb (e.g., OnGet, OnPost) within your Razor Page.
These methods are executed based on the route and constraints, without exposing the handler name in the URL.
Usage Example: Let’s say you want to have two different OnGet handlers: OnGetAsync and OnGe…