How to display JSON response into MVC view without predefining JSON structure? RRS feed

  • Question

  • I'm creating a MVC ASP.NET frontend where I am trying to display the json response of any API endpoint without predefining their structure.

    I'm trying to achieve that theJSON response from my Web API is being displayed with labels and with their values.


    public async Task<ActionResult> getCall()
        string url = "http://localhost:51080/";
        string customerApi = "customer/1";
        using (var client = new HttpClient())
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync(customerApi);
            if (response.IsSuccessStatusCode)
                string jsondata = await response.Content.ReadAsStringAsync();
                return Content(jsondata, "application/json");
            return Json(1, JsonRequestBehavior.AllowGet);

    Json reponse of this method:

    "propertyID":1,"propertyName":"Saints Castle","propertyLocation":"Emmen","propertyType":"Castle","propertyOwner":"Saint Martin"},"propertyLeaseContractID":1,"leaseContractID":1,"propertyID":1,"customerID":1}],"customerID":1,"customerFirstName":"Jan","customerLastName":"Wubs","customerGender":"Male"}

    Home/Index.cshtml View:

    ViewBag.Title = "Dashboard";
    if (Session["userID"] == null)
    <div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <!-- Labels with values here! -->
    <div class="col-md-4">
    <div class="col-md-4">

    Hope somebody got a solution for me!


    • Edited by Niels Schutte Tuesday, April 23, 2019 7:23 AM
    • Moved by CoolDadTx Tuesday, April 23, 2019 1:36 PM ASP.NET related
    Tuesday, April 23, 2019 6:43 AM

All replies

  • Hi Niels,

    According to your description, your issue is more related about ASP.NET. So, It will be more appropriate to ask your question at ASP.NET Forums.

    Thank you for your understanding.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 23, 2019 7:26 AM