简体   繁体   中英

ASP.NET Core WEB Api connect to JS

I'm on enter level with web dev, and i have very important question... When I write application in ASP.NET MVC i can return HTML view and there is an idea to create frontend for my application.

But when I use asp.net core web api I just see JSON files in my browser. My endPoints return only JSON results... and my question is:

How I can view this JSON files using HTML?

What is the way to take this JSON Files? In my WEB Api project i don't have any view folder or something like this... Can someone explain me how it works?

With ASP.NET MVC, actions return a View that is rendered into an HTML webpage.
In this case, the rendering is done on your back-end (your API server).

With ASP.NET Web API, actions return data. You'll need to write Javascript that makes a request to your API and renders the resulting data into HTML so that it can be viewed.
In this case, the rendering is done on your front-end (your user's browser).

One way to do this is. Suppose you have a json like this:

public class JsonDetail  
{  
    public string FirstName {  
        get;  
        set;  
    }  
    public string LastName {  
        get;  
        set;  
    }  
}  

// output from asp.net core c#
var json = @ "{  
    'FirstName': 'Peter', 'LastName': 'Smith'  
}";  

// then use Newtonsoft.json
var model = JsonConvert.DeserializeObject <JsonDetail> (json);  

Now, this model can be used in MVC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM