Asp.net Core 2.0中Ajax请求无法触发指定PageModel Post处理方法
Hey there! Let's figure out why your AJAX request keeps hitting the default OnPostAsync instead of your targeted OnPostFormalDealerNameAsync method. I'll break down the issues and fixes step by step.
The Core Issue: Razor Pages Handler Routing
Razor Pages uses a specific convention to map requests to handler methods. For named post handlers (like your OnPostFormalDealerNameAsync), you need to explicitly tell the framework which handler to use—either via a query parameter or route value. Your current setup isn't signaling this, so it falls back to the default OnPostAsync.
Fix 1: Update the AJAX Request to Specify the Handler
You need to add the handler parameter to your AJAX URL (or include it in the request data) to point to your named handler. Here's the adjusted JavaScript:
function getFormalDealerName(e) { var token = '@GetAntiXsrfRequestToken()'; $.ajax({ type: "POST", // Add ?handler=FormalDealerName to target your named handler url: "/ShoesMarket/Shoes2Dealer?handler=FormalDealerName", headers: { "RequestVerificationToken": token }, data: dealer_str, success: function (response) { if (response != null) { // Handle your successful response here } else { alert("Something went wrong"); } }, failure: function (response) { alert(response.responseText); }, }); }
Fix 2: Adjust the C# Handler Attributes
The [Route] attribute you added is unnecessary (and conflicting) for Razor Pages, which uses file-based routing by default. Also, the [HttpPost] attribute is redundant since the method name OnPostFormalDealerNameAsync already signals it's a POST handler. Clean up your PageModel code like this:
namespace ShoesSystem.Pages.ShoesMarket.Shoes2Dealer { [Authorize] public class IndexModel : PageModel { private readonly ShoesSystem.Data.ApplicationDbContext _context; public IndexModel(ShoesSystem.Data.ApplicationDbContext context) { _context = context; } public async Task OnGetAsync() { // Your existing GET logic } public async Task<IActionResult> OnPostAsync() { // Your existing default POST logic } [ValidateAntiForgeryToken] // Removed [Route] and redundant [HttpPost] public async Task<IActionResult> OnPostFormalDealerNameAsync() { // Your targeted handler logic here } } }
Why This Works
- The
?handler=FormalDealerNamequery parameter tells Razor Pages to look for a handler method namedOnPostFormalDealerNameAsync(the framework maps the handler value to the suffix afterOnPost). - Removing the
[Route]attribute avoids overriding the default file-based routing for your page (yourIndexModelmaps to the/ShoesMarket/Shoes2Dealerpath, matching your page file location).
Extra Notes
- If you prefer to use a route segment instead of a query parameter (e.g.,
/ShoesMarket/Shoes2Dealer/Dealers), you can update the@pagedirective in yourIndex.cshtmlfile to:
Then your AJAX URL can be@page "{handler?}"/ShoesMarket/Shoes2Dealer/Dealers(sinceDealerswill map to theFormalDealerNamehandler? Wait no—wait, the handler name needs to match. If you want the route segment to beDealers, you'd need to name your methodOnPostDealersAsync, or use a route constraint. The query parameter approach is simpler for most cases. - Ensure your
dealer_stris formatted correctly. If it's a JSON object, addcontentType: "application/json"to your AJAX call and stringify the data:data: JSON.stringify(dealer_str), contentType: "application/json",
内容的提问来源于stack exchange,提问作者Chunde Huang

