Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Razor Pages and Json

$
0
0

Hi!

I am developing an application that consults a Web API, gets data in json format and I want to simply present this data in a page.

The code below is in the App.csthml.cs file (there is more to it) and it generates the content I need:

public async Task<IActionResult> OnPostAsync(string urljson)
{
    var httpClient = new HttpClient();
    var resultado = await httpClient.GetStringAsync(urljson);

    Produto.Rootobject Produtos = JsonConvert.DeserializeObject<Produto.Rootobject>(resultado);

    return RedirectToPage("ListaSugestoes", Produtos);
}

The code I added in the page I want the results to appear is:

using Buyit.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Buyit.Pages
{
    public class ListaSugestoesModel : PageModel
    {

        private readonly Produto.Rootobject _jsonProdutos;

        public ListaSugestoesModel(Produto.Rootobject jsonProdutos)
        {
            _jsonProdutos = jsonProdutos;
        }

        public JsonResult OnGetProdutos()
        {
            return new JsonResult(_jsonProdutos);
        }
    }
} 

Is it done right? Tks!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>