-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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.
All reactions
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...
Replies: 1 comment
-
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 OnGetTestAsync. Here’s how you can set it up:
C#
public class MyPageModel : PageModel
{
public async Task OnGetAsync()
{
// Your main OnGetAsync logic here
// ...
return Page();
}
public async Task<IActionResult> OnGetTestAsync()
{
// Your specific OnGetTestAsync logic here
// ...
return Page();
}
}
AI-generated code. Review and use carefully. More info on FAQ.
Routing Configuration:
By default, Razor Pages use a naming convention to map HTTP verbs to handler methods (e.g., OnGet, OnPost).
To use named handlers, you can add an optional route value for "handler" in the @page directive at the top of your Razor Page:
@page "{handler?}"
This makes the handler name part of the route, but it won’t appear in the URL unless explicitly specified.
URLs and Execution:
When navigating to your page, you can use URLs like /MyPage (for the default OnGetAsync) or /MyPage/Test (for OnGetTestAsync).
The framework will automatically route requests to the appropriate handler based on the URL segment.
Avoiding If-Else Conditions:
With named handlers, you don’t need to use if-else conditions to determine which method to execute.
Each handler method can have its own specific logic, and the framework will handle the routing for you.
Remember that named handler methods provide a clean way to organize your code and avoid conditional checks. They allow you to define separate logic for different scenarios without exposing handler names in the URL. Happy coding! 🚀 1 2 3
Learn more
1
learnrazorpages.com
2
mikesdotnetting.com
3
stackoverflow.com
4
talkingdotnet.com
+1 more
Beta Was this translation helpful? Give feedback.