aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Trying to return json from page handler but it's returning html code (Razor Pages) #8698

Closed troncomputers closed 5 years ago

troncomputers commented 5 years ago

Is this a Bug or Feature request?:

Bug (I think)

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

Just try to return json from page handler.

public class Customer
{
     public int ID { get; set; }
     public string Code { get; set; }
     public string Name { get; set; }
}

Index.cshtml.cs

public JsonResult PageData()
{
    Customer c = new Customer();
    c.ID = 1;
    c.Code = "TEST";
    c.Name = "Test Customer Pro";

    return new JsonResult(tn);
}
//or
public IActionResult PageData()
{
    Customer c = new Customer();
    c.ID = 1;
    c.Code = "TEST";
    c.Name = "Test Customer Pro";

    return new JsonResult(tn);
}

Index.cshtml

<a asp-page-handler="PageData" class="btn btn-secondary text-primary mt-3">
   <span class="btn-icon">
       <i class="la la-plus"></i>Customer
   </span>
</a>

Description of the problem:

Page handler returns html not json. I'm trying to use DataTables helper: LZorglub/DataTables.AspnetCore.Mvc

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All: 2.1

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @troncomputers. You should use a certain naming convention so the handler is hit. Try to rename the handler to OnGetPageData but leave the asp-page-handler="PageData".

troncomputers commented 5 years ago

OMG, yes. I'm sorry...