Submit Handler Methods in Razor Pages

In our earlier Tutorial we have explained how to create a Razor Page project step by step. So now talk about handler method and Async methods in asp.net core razor page.

Asp.Net Core Razor Page has some methods that are automatically executed whenever we make a Request . There is some naming convention used by Razor Pages framework, which helps to select the appropriate handler method to execute.

Here are some standard methods of razor page codebehind [chtml.cs].

public void OnGet()
{
Message = "OnGet()";
}
 
public void OnPost()
{
Message = "OnPost()";
} 
public void OnPut()
{
Message = "OnPut()";
} 
Handler Methods in Razor Pages

Above methods are always executed when we request the page, All method name follows standard naming convention.

Default Naming Convention

default convention of page methods (that are automatically executed on page request)
"On" + HTTP verb used for the request

So all verbs are prefixed with "On" are the default methods name, Like OnGet(), OnPost(), OnPut(), OnDelete() etc.

All Handler methods in razor page also have optional asynchronous equivalents method like OnGetAsync(), OnPostAsync() etc. Now you do not need to add the Async suffix

You can have either of these two methods in same razor page codebehind [chtml.cs]
public void OnGet()
{
Message = "OnGet()";
}
Or 
public void OnGetAsync()
{
Message = "OnGetAsync()";
} 

Writing both will throw “InvalidOperationException: Multiple handlers matched” exception, because both handlers are same, one with Async one without.

Note: even if one method takes parameter and other one without parameter still same exception will be thrown, means if you have OnGet(string parameter1) and OnGetAsync() still won’t work, it’s still considered as multiple handlers on same page.

Custom methods in Razor Page application

So far we have seen how to deal with standard methods, now we learn how to write custom method in asp.net core razor page.

We can give any name to method as per our business requirement
Let's assume we need to create a registration form for company's annual function, here we create a simple registration form

<form method="post">
Name: <input type="text" name="FullName" />
<button asp-page-handler="Register">Register</button>
</form>

Now in code behind we have to write a custom method to capture the data, for method name we have to follow some naming convention, method name always has to be like "OnPost" + asp-page-handler value.
So here name should be OnPost + Register= OnPostRegister()

public void OnPostRegister()
{
    var name = Request.Form["FullName"];
// now you have all submitted values in local variables
}

We also can make Async method
"OnPost" + asp-page-handler value + Async

public void OnPostRegisterAsync()
{
    var name = Request.Form["FullName"];
    // now you have all submitted values in local variables
}

But remember we can't have both methods on same page, even with different signature, then that will throw an exception for multiple handlers.

Handling Multiple Submit Buttons In Razor Pages
Now learn how to deal with multiple submit button with real time example

Here we have designed a simple form; we want to capture the training request from our employees, who want to learn which subject! (You probably thinking this could have been done different ways, true! this just to give an example of multiple submits!)

<form method="post">
<input type="submit" value="Asp.net" asp-page-handler="Aspnet" asp-route-valuecount="10" />
<input type="submit" value="Asp.net MVC" asp-page-handler="Aspnetmvc" asp-route-valuecount="12"/>
<input type="submit" value="Asp.net Core" asp-page-handler="Aspnetcore" asp-route-valuecount="15"/>
<input type="submit" value="LINQ" asp-page-handler="Linq" asp-route-valuecount="20"/>
<input type="submit" value="HTML" asp-page-handler="Html" asp-route-valuecount="10"/>
</form>

Now in code behind you can write multiple handler method, each time form is submitted a different method is getting executed, so based method you can update database, write complex business logic to decide further action for user.

 public void OnPostAspnet(int valuecount)
        {
            ViewData["ActionMessage"] = $"Aspnet {valuecount}";
        }
        public void OnPostAspnetmvc(int valuecount)
        {
            ViewData["ActionMessage"] = $"AspnetMVC {valuecount}";
        }
        public void OnPostAspnetcore(int valuecount)
        {
            ViewData["ActionMessage"] = $"AspnetCore {valuecount}";
        }
        public void OnPostLinq(int valuecount)
        {
            ViewData["ActionMessage"] = $"LINQ {valuecount}";
        }
        public void OnPostHtml(int valuecount)
        {
            ViewData["ActionMessage"] = $"Html {valuecount}";
        }

Please make sure method naming convention is followed.

Check more about Asp.net core razor pages.


Asp.Net Core C# Examples | Join Asp.Net MVC Course