title: Example AJAX call back to an ASP.NET Core Razor Page
body: <p>I've found examples of have multiple handlers on a page and the associated naming convention (ie OnPostXXX) and 'asp-post-hanlder' tag helper. But how can I call one of these methods from an AJAX call.</p>
  
  <p>I have an older example with a typical MVC view and controller but how does this work with a Razor Page?</p>
  
  <p>For example if I take the base application and modify the About.cshtml page to the following:</p>
  
  <pre><code>@page
  @model AboutModel
  @{
      ViewData["Title"] = "About";
  }
  &lt;h2&gt;@ViewData["Title"]&lt;/h2&gt;
  &lt;h3&gt;@Model.Message&lt;/h3&gt;
  
      &lt;input type="button" value="Ajax test" class="btn btn-default" onclick="ajaxTest();"  /&gt;
  
  @section Scripts {
  &lt;script type="text/javascript"&gt;
      function ajaxTest() {
          console.log("Entered method");
          $.ajax({
              type: "POST",
              url: '/About', // &lt;-- Where should this point?
              contentType: "application/json; charset=utf-8",
              dataType: "json",
          error: function (xhr, status, errorThrown) {
              var err = "Status: " + status + " " + errorThrown;
              console.log(err);
          }
          }).done(function (data) {
              console.log(data.result);
          })
      }
  &lt;/script&gt;
  }
  </code></pre>
  
  <p>And on the model page About.cshtml.cs</p>
  
  <pre><code>public class AboutModel : PageModel
  {
      public string Message { get; set; }
  
      public void OnGet()
      {
          Message = "Your application description page.";
      }
  
      public IActionResult OnPost() {
          //throw new Exception("stop");
          return new JsonResult("");
      }
  }
  </code></pre>
  
  <p>The OnPost is not called from the Ajax call.</p>