asp.net - Razor Pages 'asp-page-handler' did not working [POST] - Stack Overflow

admin2025-04-30  0

This is working :

@page
@model test0201.Pages.IndexModel
@{
}


<form method="post">
    <button type="submit" class="btn btn-primary">Test</button>
</form>
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace test0201.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
        public async Task OnPost()
        {
            Console.WriteLine("testtesttest");
        }
    }
}


And this is Not :

<form method="post">
    <button type="submit" asp-page-handler="RefreshReturns" class="btn btn-primary">Test</button>
</form>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace test0201.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
        public async Task OnPostRefreshReturnsAsync()
        {
            Console.WriteLine("testtesttest");
        }
    }
}


Handler just not working. ChatGPT and Google didnt help. What i am missing here? First i thought my method not working, so i tried to test it and put just Console.Writeline.

This is working :

@page
@model test0201.Pages.IndexModel
@{
}


<form method="post">
    <button type="submit" class="btn btn-primary">Test</button>
</form>
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace test0201.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
        public async Task OnPost()
        {
            Console.WriteLine("testtesttest");
        }
    }
}


And this is Not :

<form method="post">
    <button type="submit" asp-page-handler="RefreshReturns" class="btn btn-primary">Test</button>
</form>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace test0201.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
        public async Task OnPostRefreshReturnsAsync()
        {
            Console.WriteLine("testtesttest");
        }
    }
}


Handler just not working. ChatGPT and Google didnt help. What i am missing here? First i thought my method not working, so i tried to test it and put just Console.Writeline.

Share Improve this question edited Jan 8 at 1:20 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Jan 4 at 21:01 SerjeqSerjeq 32 bronze badges 4
  • Please check whether you have imported tag helper in your _ViewImports.html: @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers. Otherwise, you need to include this line in your razor page view. Also, you can try to debug to see the rendered HTML for <button type="submit" asp-page-handler="RefreshReturns" class="btn btn-primary">Test</button>. And share it in the question. Thanks. – Yong Shun Commented Jan 5 at 0:44
  • Your code works for me. If you didn't have TagHelpers imported both of your forms would fail. To diagnose something like this, you should look at the generated HTML, like @YongShun said, and also at the request sent by the form (in your browser console). If that doesn't help, you should add both of those things to your question. – BenderBoy Commented Jan 6 at 14:51
  • One thing to note is that ASP's Form TagHelper is a little fickle with the CSRF input. In your example code, everything is fine, but if your form had an action attribute with ANY non-empty value, the CSRF input would not get generated, resulting in a 400. Perhaps you have something like this in your actual code? – BenderBoy Commented Jan 6 at 14:54
  • 1 Thanks @YongShun. "@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers" - it helps.
转载请注明原文地址:http://anycun.com/QandA/1746027735a91543.html